c# - Autofac单例处理

标签 c# dependency-injection owin autofac

我只想确定 Autofac 中的单例处理。如果我在 Autofac 中将一个组件注册为单例,它是否会在应用程序关闭时触发 Dispose 方法?

目前,我将组件保存在 Startup 中,并在 OnShutDown 中调用它,我在 OnAppDisposing 中注册了它。认为用 Autofac

设置这个东西会更优雅

最佳答案

documentation说:

If you have singleton components (registered as SingleInstance()) they will live for the life of the container. Since container lifetimes are usually the application lifetime, it means the component won’t be disposed until the end of the application.

> http://docs.autofac.org/en/latest/lifetime/disposal.html

如果您希望 AutofacSingleInstance 注册上调用 Dispose 方法,您必须调用 Dispose应用程序关闭时容器的方法。

这个 github 问题讨论:Should UseAutofacMiddleware dispose container on OWIN shutdown .

Autofac Owin Integration 的测试版中,您有一个 DisposeContainerOnShutdown 扩展方法。

Autofac.Owin 4.0 版本开始,您可以这样做:

app.DisposeContainerOnShutdown(container);

对于旧版本,您可以复制/粘贴 AutofacAppBuilderExtensions.cs 中的 DisposeContainerOnShutdown 方法源代码

关于c# - Autofac单例处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35411179/

相关文章:

c# - 我可以在函数内部声明一个函数指针吗?

c# - Owin 自托管 - 限制请求的最长时间

c# - 如何等待C#中的ADB输出?

c# - 32 位快速统一哈希函数。使用 MD5/SHA1 并截掉 4 个字节?

c# - Unity 和 WCF 库 : Where to load unity in a wcf library?

asp.net - OWIN token 身份验证 400 来自浏览器的 OPTIONS 的错误请求

asp.net-mvc - ASP.NET MVC 5 : Custom Login URL based on localized route parameter

scala - Scaldi 和 Guice 和有什么不一样

c# - Blazor、对象生命周期和依赖注入(inject)

php - 在 Laravel 中通过依赖注入(inject)解决抽象类的依赖