At first I did confidentially suppose that I could understand it, but via some simple example with Autofac
, it appeared that I might understand it wrong, here is the code that I've tried:
//register the service
autofacBuilder.RegisterType<MyService>()
.As<IMyService>().InstancePerLifetimeScope();
//testing code
void _test1()
{
var myService = autofacContainer.Resolve<IMyService>();
}
void _test2()
{
_test1();
var myService = autofacContainer.Resolve<IMyService>();
}
Test it by running _test2()
and you can simply check the instances resolved in the 2 methods.
So with the code above, I understand the myService
in _test1
and myService
in _test2
should be different. Because I think the lifetime scope of myService
in _test1
should be just in that method while the lifetime scope of myService
in _test2
should be in _test2
as well. So we have 2 different scopes here, but somehow the resolved instances of myService
are the same one.
So could you please explain that issue to me, what does lifetime scope
exactly mean here? inside one same class? or something even larger?