Unity and Random “Index was outside the bounds of the array” exception

Jul 18, 2014 at 10:01 AM
We are running web site with around 15.000 realtime user (google analytics) (Around 1000 request/sec (perf counters)).

We have two web server behind load balancer.

Sometimes every day sometimes 1 time in a week one of our web servers stop execute requests and start to response with error and every request is logging following exception: "System.IndexOutOfRangeException - Index was outside the bounds of the array."

Our environment : IIS 8.5, .Net 4.5.0, Mvc 5.1.0, Unity 3.5 (same state with 3.0), WebActivatorEx 2.0 In IIS, Worker Process 1 and other settings are with defaults.

We could not catch any specific scenario made this error. After App pool recycle everything start with no problem. And before every request respond with error, there is not any error related with it.

There is one question asked in the past with related old Unity version: https://unity.codeplex.com/discussions/328841 http://unity.codeplex.com/workitem/11791 Could not see anything I can do about it.

Here exception details:
System.IndexOutOfRangeException
Index was outside the bounds of the array.

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   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 DemoSite.News.Portal.UI.App_Start.UnityConfig.<>c__DisplayClass1.<RegisterTypes>b__0()
   at DemoSite.News.Portal.Core.Controller.BaseController.Initialize(RequestContext requestContext)
   at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
   at System.Web.Mvc.Async.AsyncResultWrapper.Begin[TState](AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Maybe it is related with high traffic. Is there anyone encounter a problem like that and any solution ?

Thanks in advance
Jul 19, 2014 at 1:47 PM
This problem is caused by a bug/design flaw in Unity's UnityServiceLocator class. More details can be found here. IMO this is something that the Unity team needs to address.
Jul 21, 2014 at 9:35 AM
Jul 21, 2014 at 9:39 AM
Edited Jul 21, 2014 at 9:41 AM
Hi,
Thank you for your reply.
I was the one asked same question in stackoverflow.
http://stackoverflow.com/questions/24822232/unity-and-random-index-was-outside-the-bounds-of-the-array-exception

And I think solution is using DependencyResolver comes with Mvc 3+ instead of ServiceLocator.
Problem is happening every few days. So we will make changes today and will wait for a while if it is happens again.

Old usage:
ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
and new one
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
Jul 23, 2014 at 2:24 PM
If you can prevent making any direct calls into ServiceLocator.Current or DependencyResolver.Current, since this a well-known anti-pattern.
Jul 23, 2014 at 6:30 PM
Yes I know, some people agree with it and some disagree explained with their reason.

But is there a way to inject dependencies in a attribute / filter in a mvc project ?

Maybe attribute usage is another anti-pattern since it is hard to write its test but this is another subject.
Jul 25, 2014 at 8:13 PM
Yep, injecting dependencies into attributes is another anti-pattern, as described here and here.