c# – Autofac singleton dispose

我只是想确保在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.

> 07001

如果希望Autofac在SingleInstance注册上调用Dispose方法,则必须在应用程序关闭时调用容器的Dispose方法.

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

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

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

app.DisposeContainerOnShutdown(container);

使用旧版本,您可以从AutofacAppBuilderExtensions.cs源代码复制/粘贴DisposeContainerOnShutdown方法

上一篇:c# – 如何在AutoMock(Moq)中使用Lazy <>


下一篇:ASP.NET MVC IOC 之 Autofac(三)-webform中应用