autofac / Autofac

An addictive .NET IoC container
https://autofac.org
MIT License
4.44k stars 836 forks source link

Configure Autofac so that it's ExternallyOwned for default, and "InternallyOwned" for separate services #1407

Closed WindingWinter closed 5 months ago

WindingWinter commented 5 months ago

Currently how Autofac works is that for the IDisposable type service, Autofac container will manage the disposing, unless it is being set as ExternallyOwned. Autofac does this by placing it on the Disposal queue and call the service when the container is being disposed. This will create a strong reference to the service until the container is disposed.

While I can see the merit of this design, for my use case I would prefer the inversion of it, namely, I want to configure that for default, the IDisposable services that are owned by Autofac are ExternallyOwned ( I'm responsible for calling the Dispose method), and only for a handful of services are "InternallyOwned" ( and I would have to mark them explicitly as such), meaning that for these services, Autofac will be responsible for calling the Dispose method.

Is it possible to do have this API? Or this is already doable in the current Autofac engine?

tillig commented 5 months ago

Duplicate #481, #998. This isn't something the Autofac team is going to add. See the referenced issues for more info.