Duplicate objects detected for instance X of type Y

I get the following exception to the structure structure - "Duplicate objects detected for instance d54e25dc-d19c-4d70-99d1-56bd2502d203 type ..."

Stack trace:

at StructureMap.InstanceCache.Set(Type pluginType, Instance Instance, Object result) in c:\code\structuremap\Source\StructureMap\InstanceCache.cs:line 60
at StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 159
at StructureMap.BuildSession.<>c_DisplayClass3.<.ctor>b_1() in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 34
at StructureMap.BuildSession.CreateInstance(Type pluginType) in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 192
at StructureMap.BuildSession.GetInstance[T]() in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 78

This exception occurs only from time to time and is very difficult to reproduce.

What I have found out so far:

  • The call ObjectFactory.Container.GetInstance(type)will create a new one BuildSessionwith a new non-static cache.
  • The call will reach BuildSession.CreateInstance(Type pluginType, Instance instance), and the instance is not yet in the cache. ( https://github.com/structuremap/structuremap/blob/master/Source/StructureMap/BuildSession.cs#L149-166 )
  • Now we go to _builder.Resolve(pluginType, instance, this);
  • Somewhere during the time, the Resolve()object will go to the cache, and then in CreateInstancewe will save it to the cache again, having received an exception.

Singleton. - WCF, IIS.

.

+3

All Articles