我只想确定 Autofac 中的单例处理。如果我在 Autofac 中将一个组件注册为单例,它是否会在应用程序关闭时触发 Dispose
方法?
目前,我将组件保存在 Startup
中,并在 OnShutDown
中调用它,我在 OnAppDisposing
中注册了它。认为用 Autofac
最佳答案
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.
如果您希望 Autofac 在 SingleInstance
注册上调用 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/