Autofac实例生命周期

1.默认,每次请求都会返回一个实例

builder.RegisterType<X>().InstancePerDependency();
View Code

2.Per Lifetime Scope:这个作用域适用于嵌套的生命周期。一个使用Per Lifetime 作用域的component在一个 nested lifetime scope内最多有一个实例。

builder.RegisterType<X>().InstancePerLifetimeScope();

  

3.基于线程或者Context上下文的请求,返回一个单例实例,在Controller的一个View页面执行时包含了整个Context上下文处理,这种属于第二种情况的一个例子

   builder.RegisterType<OpenIdRelyingPartyService>().As<IOpenIdRelyingPartyService>().InstancePerHttpRequest();

4.单例模式:所有的请求只会返回一个单例

builder.RegisterType<X>().SingleInstance();

  Don’t resolve from the root container. Always resolve from and then release a lifetime scope.

原文地址:https://www.cnblogs.com/xiaoweinet/p/3512255.html