title: Unity and Random "Index was outside the bounds of the array" exception
body: <p>We are running web site with around 15.000 realtime user (google analytics) (Around 1000 request/sec (perf counters)).</p>
  
  <p>We have two web server behind load balancer.</p>
  
  <p>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."</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>There is one question asked in the past with related old Unity version:
  <a href="https://unity.codeplex.com/discussions/328841" rel="nofollow">https://unity.codeplex.com/discussions/328841</a>
  <a href="http://unity.codeplex.com/workitem/11791" rel="nofollow">http://unity.codeplex.com/workitem/11791</a>
  Could not see anything I can do about it.</p>
  
  <p>Here exception details:</p>
  
  <pre><code>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.&lt;&gt;c__DisplayClass1.&lt;RegisterTypes&gt;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.&lt;BeginProcessRequest&gt;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&amp; completedSynchronously)
  </code></pre>
  
  <p>My configuration is as follows:</p>
  
  <pre><code>public static void RegisterTypes(IUnityContainer container) 
  { 
      var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
      container.LoadConfiguration(section); 
      ServiceLocator.SetLocatorProvider(() =&gt; new UnityServiceLocator(container));
  }
  </code></pre>
  
  <p>Initialize method as follow:</p>
  
  <pre><code>protected override void Initialize(System.Web.Routing.RequestContext requestContext)
  {
      if (requestContext.RouteData.Values["ViewActionId"] != null)
      {
          int viewActionId;
          if (!int.TryParse(requestContext.RouteData.Values["ViewActionId"].ToString(), out viewActionId))
              return;
  
          var cacheProvider = ServiceLocator.Current.GetInstance&lt;ICacheProvider&gt;();
          List&lt;ViewActionClass&gt; viewActionClasses = null;
          string cacheKey = CacheKeyCompute.ComputeCacheKey("ViewActionClass", CacheKeyTypes.DataCache,
              new KeyValuePair&lt;string, string&gt;("viewActionId", viewActionId.ToString()));
  
          _configuration = ServiceLocator.Current.GetInstance&lt;IConfiguration&gt;();
  
          viewActionClasses = 
              cacheProvider.AddOrGetExistingWithLock&lt;List&lt;ViewActionClass&gt;&gt;(cacheKey, () =&gt;
          {
              var viewActionClassBusiness = 
              ServiceLocator.Current.GetInstance&lt;IViewActionClassBusiness&gt;();
  
              return viewActionClassBusiness.ViewActionClassGetByViewActionId(viewActionId);
          });
  
          ViewBag.ActionClass = viewActionClasses;
          ViewBag.Configuration = _configuration;
      }
      base.Initialize(requestContext);
  }
  </code></pre>
  
  <p>Registration xml for ICacheProvider, IConfiguration and IViewActionClassBusiness</p>
  
  <pre><code>&lt;type type="DemoSite.Runtime.Caching.ICacheProvider, DemoSite.Core"
          mapTo="DemoSite.Runtime.Caching.ObjectCacheProvider, DemoSite.Core"&gt;
      &lt;lifetime type="containerControlledLifetimeManager" /&gt;
    &lt;/type&gt;
  &lt;type type="DemoSite.Core.Configuration.IConfiguration, DemoSite.Core"
          mapTo="DemoSite.Core.Configuration.ConfigFileConfiguration, DemoSite.Core"&gt;
      &lt;lifetime type="containerControlledLifetimeManager" /&gt;
    &lt;/type&gt;
  &lt;type type="DemoSite.News.Business.IViewActionClassBusiness, DemoSite.News.Business"
          mapTo="DemoSite.News.Business.Default.ViewActionClassBusiness, DemoSite.News.Business.Default"&gt;
      &lt;lifetime type="perRequestLifetimeManager" /&gt;
    &lt;/type&gt;
  </code></pre>
  
  <p>Maybe it is related with high traffic.
  Is there anyone encounter a problem like that and any solution ?</p>
  
  <p>Thanks in advance</p>