-
-
Notifications
You must be signed in to change notification settings - Fork 838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IDisposable Instance is not disposed unless it is activated/resolved #780
Comments
I was talking to @alexmg about this and I think you found a bug... but in the inverse of what you're mentioning. Which is to say: Autofac should be responsible for disposing things that it creates; but if you create the instance, you should also be responsible for disposing it... otherwise it could well get "disposed out from under you." We'll have to look at that. Minor note, somewhat unrelated: In your test, you should register the instance when you begin the lifetime scope rather than using using (var scope = container.BeginLifetimeScope(c => c.RegisterInstance(dep)))
{
// scope.Resolve<ShouldDispose>();
} |
@tillig Thanks for the info and sorry for the very slow reply on my part! I would be ok with having to dispose instances that I create, as long as it's consistent whether they are referenced or not. But an important note regarding the use of |
There is an interesting comment related to this in #383 which discusses the disposal of provided instances.
I can understand that with an explicit opt-out option in We actually have tests around disposing provided instances. This one ensures that a provided instance is disposed along with the container even when not resolved.
And this one ensures it is only disposed once in the case it is resolved.
I added another test similar to the first but using an action provided to
This test does not pass even when both the lifetime scope and container are disposed. The decision now is do we try to make the behaviour consistent or just make all provided instances externally owned regardless of whether or not that has been requested with |
I'm starting to think that if you create it, you're responsible for disposing of it. We've seen that pattern in lifetime scopes, for example: If Autofac integration libraries create, say, a per-request scope, Autofac integration libraries are also responsible for cleaning it up; if you create a lifetime scope, you're responsible for cleaning it up. That's why we didn't resolve #397 by tracking nested lifetime scopes - you're responsible for cleaning up things you create. Likewise, I can see from issues like autofac/Autofac.Extensions.DependencyInjection#15 that it can be confusing if you dispose a container and have it dispose instances out from under you. The container didn't create that thing, why is it disposing it? I'd be up for making instance handling 100% externally owned across the board. |
What do you think about a opt-in for having Autofac disposing instances I create? It may be hard to know how/when to dispose some objects I had to create, being able to ask Autofac to do it for me seems a good feature. |
This fix is included in 4.6.1 which has been pushed to NuGet. |
Hi `
` |
The net result of this issue: Once you give Autofac the instance, it will consider itself the owner of the instance lifetime and will dispose the instance when the owning lifetime scope is disposed unless you specifically register it as |
Ok. Thanks a bunch. |
For future readers this is also explained in the documentation. |
The issue is clearly illustrated by this failing XUnit test:
Autofac version is 3.5.2.
The text was updated successfully, but these errors were encountered: