ArgumentOutOfRangeException in UnityContainer.Dispose

Nov 26, 2009 at 4:36 PM

I've created a per-request child container and I'm disposing of it in Application_EndRequest.

We are stress testing the application and finding occasional exceptions in the call to Dispose:

System.ArgumentOutOfRangeException occurred
  Message="Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
  Source="mscorlib"
  ParamName="index"
  StackTrace:
       at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
       at System.ThrowHelper.ThrowArgumentOutOfRangeException()
       at System.Collections.Generic.List`1.RemoveAt(Int32 index)
       at System.Collections.Generic.List`1.Remove(T item)
       at Microsoft.Practices.ObjectBuilder2.LifetimeContainer.Remove(Object item)
       at Microsoft.Practices.Unity.UnityContainer.Dispose(Boolean disposing)
       at Microsoft.Practices.Unity.UnityContainer.Dispose()
...

Having looked at the source code, I think this is a synchronisation problem with the "items" list in LifetimeContainer. I added a lock on "items" in LifetimeContainer.Remove and this solved the issue for me.

Is there something I could do without altering the Unity source code?

Nov 27, 2009 at 6:09 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.