This project is read-only.

IDisposable.Dispose() called multiple times at end of lifetime scope

Jun 23, 2012 at 9:47 AM
Edited Jun 23, 2012 at 9:55 AM

Hi, perhaps this is by design but i just want to confirm the below:

For every GetInstance() that resolves a type which has a common lifetime scoped object (such as a UnitOfwork), when the scope ends if the UnifOfWork implements IDisposable, dispose is called for every time a service dependent on this was resolved during the scope.

Is this correct? Is the reason it is called multiple times incase we are doing something exotic in our dispose method?




using (container.BeginLifetimeScope()){
    var serviceA = container.GetInstance();
    var serviceB = container.GetInstance();
    var unitOfWork = container.GetInstance(); 
Jun 23, 2012 at 11:06 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jun 23, 2012 at 11:09 PM

I can confirm this is a bug.

Note that this shouldn't affect your application, since the contract of IDisposable states that an implementation should allows Dispose to be called multiple times, but this is nonetheless quite annoying and can decrease performance. Registration for disposal happens at the wrong place in the code, which causes this bug. The bug will be fixed in the next release.

Thanks for taking the time to report this.

Jul 3, 2012 at 10:05 PM

I just published a new package of the Simple Injector Lifetime Scoping Extensions NuGet package (version that fixes this bug.

Marked as answer by dot_NET_Junkie on 11/4/2013 at 1:56 AM