KeyNotFoundException in Multi Thread Scenario

Nov 15, 2010 at 6:50 PM

Hi, I'm using unity inside a Parallel.For Loop, and sometimes an exception is throwed


Type : System.Collections.Generic.KeyNotFoundException, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089
 Message : The given key was not present in the dictionary.
 Source : mscorlib
 Help link :
 Data : System.Collections.ListDictionaryInternal
 TargetSite : TValue get_Item(TKey)
 Stack Trace :    at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
    at Microsoft.Practices.Unity.NamedTypesRegistry.RegisterType(Type t, String name)
    at Microsoft.Practices.Unity.UnityDefaultBehaviorExtension.OnRegister(Object sender, RegisterEventArgs e)
    at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
    at Microsoft.Practices.Unity.UnityContainer.RegisterType(Type from, Type to, String name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers)
    at Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType[TFrom,TTo](IUnityContainer container, InjectionMember[] injectionMembers)

Any ideas, about how to solve it?


Nov 15, 2010 at 7:49 PM

Calling RegisterType on the container is explicitly NOT thread safe. You'll need to add locking yourself.