c#-构造函数注入Autofac的InstancePerDependency的生存期

我对依赖生存期的概念很陌生.我已经阅读了有关different lifetime types的文档.

对于InstancePerDependency,是默认值.该页面讨论如何在Lifetimescope中进行解析,以便容器可以正确处理它.我能理解

但是,当Autofac通过构造函数自动注入依赖项时该怎么办.网络请求完成后,容器仍保留引用吗?

解决方法:

This documentation帮助.

Note that with Autofac integration libraries standard unit-of-work
lifetime scopes will be created and disposed for you automatically.
For example, in Autofac’s ASP.NET MVC integration, a lifetime scope
will be created for you at the beginning of a web request and all
components will generally be resolved from there. At the end of the
web request, the scope will automatically be disposed – no additional
scope creation is required on your part. If you are using one of the
integration libraries, you should be aware of what
automatically-created scopes are available for you.

换句话说,如果Autofac正在创建控制器来处理Web请求(如果注入了依赖项,则必须如此),则依赖关系将使用为每个控制器创建的作用域进行解析,并在Web请求结束时进行处理.

上一篇:C#-有状态服务的依赖注入


下一篇:c#-Autofac模块应该注册自己的从属模块吗?