起初我确实秘密地认为我可以理解它,但是通过一些简单的Autofac
示例,似乎我可能理解错了,这是我尝试过的代码:
//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>();
}
通过运行_test2()
进行测试,您只需检查 2 种方法中解析的实例即可。
因此,使用上面的代码,我了解_test1
中的myService
和_test2
中的myService
应该不同。因为我认为myService
在_test1
中的生命周期范围应该只是在这种方法中,而_test2
中myService
的生命周期范围也应该在_test2
中。所以我们这里有2 个不同的作用域,但不知何故,myService
的已解决实例是相同的。
那么,您能否向我解释一下这个问题,lifetime scope
在这里到底是什么意思? 在同一堂课内? 还是更大的东西?
你混淆了 c# 作用域和 autofac 的作用域。这就像比较苹果和栅栏。:)它们只是不同,彼此无关。
因此,为了澄清这一点,请查看下面的基本示例。请注意,如果您是启动示波器的人,则实际上应该由您销毁,就像示例 1 中所做的那样。在其他示例中,为了简洁起见,我跳过了它。
// example 1
autofacBuilder.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope();
var container = autofacBuilder.Build();
void _test1(IComponentContext scope){
var myService = scope.Resolve<IMyService>();
}
void _test2(IComponentContext scope){
// the same scope is used here and in _test1()
// this means that the service instance will be the same here and there
_test1(scope);
var myService = scope.Resolve<IMyService>();
}
// it's only here that DI lifetime scope starts
using (var scope = container.BeginLifetimeScope()) {
_test2(scope);
}
// example 2
autofacBuilder.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope();
var container = autofacBuilder.Build();
void _test1(IComponentContext scope){
var myService = scope.Resolve<IMyService>();
}
void _test2(IComponentContext scope){
// now new scope is used in _test1() call
// this means that instances will be different here and there since they are resolved from different scopes
_test1(scope.BeginLifetimeScope());
var myService = scope.Resolve<IMyService>();
}
var scope = container.BeginLifetimeScope();
_test2(scope);
// example 3
// NOTE THIS!
autofacBuilder.RegisterType<MyService>().As<IMyService>().InstancePerDependency();
var container = autofacBuilder.Build();
void _test1(IComponentContext scope){
var myService = scope.Resolve<IMyService>();
}
void _test2(IComponentContext scope){
// the same scope is used here and in _test1()
// but now service instances will be different even though they are resolved from the same scope
// since registration directs to create new instance each time the service is requested.
_test1(scope);
var myService = scope.Resolve<IMyService>();
}
var scope = container.BeginLifetimeScope();
_test2(scope);
// example 4
autofacBuilder.RegisterType<MyService>().As<IMyService>().SingleInstance();
var container = autofacBuilder.Build();
void _test0(IComponentContext scope){
var myService = scope.Resolve<IMyService>();
}
void _test1(IComponentContext scope){
_test0(scope.BeginLifetimeScope());
var myService = scope.Resolve<IMyService>();
}
void _test2(IComponentContext scope){
// all the scopes used here and in other calls are different now
// but the service instance will be the same in all of them even though it is requested from different scopes
// since registration directs to get the same instance each time the service is requested regardless of the lifetime scope.
_test1(scope.BeginLifetimeScope());
var myService = scope.Resolve<IMyService>();
}
var scope = container.BeginLifetimeScope();
_test2(scope);