0

起初我确实秘密地假设我可以理解它,但是通过一些简单的例子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(),您可以简单地检查两种方法中解析的实例。

所以通过上面的代码,我理解myServicein_test1myServicein_test2应该是不同的。因为我认为 in 的生命周期范围myService应该_test1在那个方法中,而 in 的生命周期范围也myService应该_test2_test2。所以我们在这里有2 个不同的范围,但不知何故,解析的实例myService是同一个。

那么您能否向我解释一下这个问题,lifetime scope 这里到底是什么意思?在同一个班级里?或者更大的东西?

4

1 回答 1

2

您混淆了c# scopesautofac 的 scopes。这就像比较苹果和栅栏。:) 它们只是不同,彼此无关。

因此,为了澄清它,请查看下面的基本示例。请注意,如果您是像示例 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);
于 2018-06-01T13:44:00.537 回答