Autofac单例处理
Autofac singleton dispose
我只想确定 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);
对于旧版本,您可以copy/paste AutofacAppBuilderExtensions.cs 源代码中的 DisposeContainerOnShutdown
方法
我只想确定 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);
对于旧版本,您可以copy/paste AutofacAppBuilderExtensions.cs 源代码中的 DisposeContainerOnShutdown
方法