Say I have an instance of an object, resolved by Autofac like this:
ILifetimeScope scope = rootContainer....
var myService = scope.Resolve(myServiceType);
Now all I have with me is myService
. Is there any Autofac API which takes in my variable myService
and disposes it off (along with all its dependencies that Autofac resolved) ?
For e.g.
// later at some point, something like this
rootContainer.DisposeOff(myService) // <-- this should dispose the lifetime scope of myService, i.e. dispose myService, along with its other dependencies.
Few points to note:
- I cant of course just do
myService.Dispose()
myself because that won't dispose its child dependencies that Autofac injected (because Autofac is controlling their lifetimes). - I only have
myService
with me. It's the peculiar nature of the library I am dealing with. I can of course store thescope
variable myself and manage it. That would be my last resort. Just wondering if Autofac has something built into itself.