This project is read-only.


IndexOutOfRangeException After ApplicationPool Recycle


<p>It looks like the List within DependencyResolverTracker.AddResolverKey is used in a way which is possibly not be thread-safe.</p> <p>&nbsp;</p> <p>This caused us many IndexOutOfRangeExceptions randomly after an application pool recycle. Simply recycling the application pool again resolved the issue.</p> <p>&nbsp;</p> <p>Details of the stack are</p> <p>&nbsp;</p> <p>Exception is: IndexOutOfRangeException - Index was outside the bounds of the array. &amp;#xD;&amp;#xA;-----------------------------------------------&amp;#xD;&amp;#xA; At the time of the exception, the container was:&amp;#xD;&amp;#xA;&amp;#xD;&amp;#xA; Resolving Fred.Services.Person.ViewListing,ServiceHost (mapped from Fred.Services.Person.IViewListing, ServiceHost)&amp;#xD;&amp;#xA; ----&amp;gt; System.IndexOutOfRangeException: Index was outside the bounds of the array.&amp;#xA; at System.Collections.Generic.List`1.Add(T item)&amp;#xD;&amp;#xA; at Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase`1.CreateSelectedConstructor(IBuilderContext context, IPolicyList resolverPolicyDestination, ConstructorInfo ctor) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPl...).&quot; time=&quot;2012-02-16T21:14:59.0249474Z&quot;&gt;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>See this discussion for further info on it.</p> <p></p> <p>&nbsp;</p> <p>Thanks</p> <p>Rick</p>
Closed Mar 13, 2014 at 1:05 AM by bennage
Resolved in Unity 3


gmelnik wrote May 16, 2012 at 3:42 PM

Confirmed. Will fix in the next refresh of Unity.

An interim workaround: do a controlled first Resolve of all your objects so that the build plans are "primed" in the container, maybe while starting the app. These instances would be meant to be discarded, and resolving only the top level objects would do.

Almas wrote Jan 30, 2013 at 11:53 AM

Received the same exception on the web application running 4/.Net 4.5, Unity 2.1.505.2

Is the problem still not solved?

Message:    Index was outside the bounds of the array.

Stack:  at System.Collections.Generic.List`1.Enumerator.MoveNext()at System.Linq.Enumerable.WhereListIterator`1.MoveNext()at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)at Microsoft.Practices.Unity.NamedTypesRegistry.RegisterType(Type t, String name)at Microsoft.Practices.Unity.UnityDefaultBehaviorExtension.OnRegisterInstance(Object sender, RegisterInstanceEventArgs e)at System.EventHandler`1.Invoke(Object sender, TEventArgs e)at Microsoft.Practices.Unity.UnityContainer.RegisterInstance(Type t, String name, Object instance, LifetimeManager lifetime)at Microsoft.Practices.Unity.UnityContainerExtensions.RegisterInstance[TInterface](IUnityContainer container, TInterface instance, LifetimeManager lifetimeManager)at MyApp.UnityContainerConfiguratorBase.c__DisplayClass1.b__0() in c:\Builds\1\MyApp\Sources\MyApp\UnityContainerConfiguratorBase.cs:line 15at Saon.IrishJobs.WebUI.EducationSeoHandler.Handle() in c:\Builds\1\MyApp\Sources\WebUI\Infrastructure\Handler\EducationSeoHandler.cs:line 73at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

gmelnik wrote Jan 30, 2013 at 4:12 PM

Almas, Unity 3.0 hasn't shipped yet. ETA - spring 2013.Grigori

Almas wrote Jan 30, 2013 at 6:16 PM

I'm sorry, mistake.Actually site is running on .Net 4.

Almas wrote Jan 31, 2013 at 1:40 PM

Seems I found the root of the issue. It is related to construct which creates and sets UnityServiceLocator in the application.

ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));

Registration is called in UnityServiceLocator constructor. So when multiple threads were trying to access service locator, this issue raised.

Similar issues are explained well in the article: