categoryRepository.Get = An item with the same key has already been added

Member since: 2007

Upgrade from CMS 7.19.2 to 8.11

I have this problem in initialisation of an ContentProvider

CODE in Constructor: myForestCategory = ServiceLocator.Current.GetInstance<CategoryRepository>().Get("MYCATEGORY");

It is the Get function not working. Why? "MYCATEGORY" exists as a Category

[ArgumentException: An item with the same key has already been added.]
   System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +14648971
   System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) +267
   EPiServer.DataAbstraction.Category.FindChild(String name) +304
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +248
   System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source, Func`2 predicate) +215
   EPiServer.DataAbstraction.Category.FindChild(String name) +684
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +248
   System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source, Func`2 predicate) +215
   EPiServer.DataAbstraction.Category.FindChild(String name) +684
   Externweb.Web.Templates.Public.Classes.ContentProviders.NewsForMyForestProvider..ctor(IdentityMappingService identityMappingService) in c:\workspace\Holmen\Holmen.Externweb.Web\Templates\Public\Classes\ContentProviders\NewsForMyForestProvider.cs:47
   lambda_method(Closure , IArguments ) +113
   StructureMap.Construction.<>c__DisplayClass2.<CreateBuilder>b__0(IArguments args) +27
   StructureMap.Construction.InstanceBuilder.BuildInstance(IArguments args) +15
   StructureMap.Pipeline.ConstructorInstance.Build(Type pluginType, BuildSession session, IInstanceBuilder builder) +125

[StructureMapException: StructureMap Exception Code:  207
Internal exception while creating Instance '37a477ba-00e5-44ad-a6e1-5e4f44e7711a' of PluginType Holmen.Externweb.Web.Templates.Public.Classes.ContentProviders.NewsForMyForestProvider.  Check the inner exception for more details.]
   StructureMap.Pipeline.ConstructorInstance.Build(Type pluginType, BuildSession session, IInstanceBuilder builder) +621
   StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) +87
   StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) +50
   StructureMap.Pipeline.ObjectBuilder.Resolve(Type pluginType, Instance instance, BuildSession session) +147
   StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) +72
   StructureMap.BuildSession.CreateInstance(Type pluginType) +23
   EPiServer.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) +70

[ActivationException: Activation error occurred while trying to get instance of type NewsForMyForestProvider, key ""]
   EPiServer.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) +181
   EPiServer.Core.ContentProviderMap.CreateProviderInstance(ProviderSettings providerSettings) +412
   EPiServer.Core.ContentProviderMap.AddProvider(ProviderSettings providerSetting) +157
   EPiServer.Core.ContentProviderMap.LoadProviders(ProviderSettingsCollection pageProvidersCollection) +126
   EPiServer.Initialization.<>c__DisplayClass5.<Initialize>b__0() +226
   System.Threading.Tasks.Task.Execute() +110

#131736 Aug 04, 2015 14:00
  • Member since: 2011

    If you execute the same code later somewhere (let's say in page's controller action).. Do you see the same error?

    #131757 Aug 04, 2015 22:16
  • Member since: 2007

    Hi Valdis,

    If i move the code to later when i use it. it initialize but it is still not working later. Same error. Any Idea?

    #131764 Aug 05, 2015 8:16
  • Member since: 2007

    This is probably a bug after breaking changes in CMS 8. http://world.episerver.com/documentation/Items/Upgrading/EPiServer-CMS/8/Breaking-changes/changes-to-the-category-api/

    ServiceLocator.Current.GetInstance<CategoryRepository>().Get(6916); //Works

    if I iterate ServiceLocator.Current.GetInstance<CategoryRepository>().GetRoot().FindChild("MYCATEGORY");// Throws error

    if I iterate ServiceLocator.Current.GetInstance<CategoryRepository>().GetRoot().Categories i find my category

    ServiceLocator.Current.GetInstance<CategoryRepository>().Get(AnyCategoriInTop) //works

    We have totaly 396 Categories.

    Bug Episerver?

    #131801 Aug 05, 2015 15:27
  • Member since: 2007

    I got this from Episerver Support:

    The problem occurred because you have duplicate category names under the same node. Please remove them (either in the Admin mode, or directly from the table tblContent in the database). You can check the duplicated category names with this SQL command:

    SELECT
    CategoryName, fkParentId, COUNT(CategoryName)
    FROM
    [dbHolmen_v8].[dbo].[tblCategory]
    GROUP BY
    CategoryName, fkParentId
    HAVING 
    COUNT(CategoryName) > 1

    #133108 Aug 26, 2015 7:41