问题
Does Autofac support implicit lifetime scopes for the use of a factory? For example, I would like to be able to do the following
public class Session : IDisposable
{
public Session(A a, B b, C c)
{
...
}
}
...
using (var session = _sessionFactory())
{
...
}
and then upon each call to the _sessionFactory have Autofac automatically create a nested lifetime scope?
回答1:
Yes. Let the factory return Owned<Session>
. Owned instances indicates that the calling code is responsible for disposing the service. Actually, if you resolve a Func<Owned<Session>>
from the container, Autofac will give you an automatic factory that produces such instances.
来源:https://stackoverflow.com/questions/14544952/can-i-create-an-implicit-lifetime-scope-within-a-factory