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:
Message="Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
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)
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?