我似乎无法找到一个答案,在文档或计算器这个问题(虽然我可能忽略了它)。我很好奇,是否应该手动处理由ContainerBuilder提供的IContainer?Autofac - 我需要在MVC global.asax中手动处理IContainer吗?
下面是Remember.Web一个代码示例:
//etc..
IContainer container = builder.Build();//returns IDisposable instance
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
//etc..
,但我很好奇,如果它应该是这样的:
public class MvcApplication : HttpApplication
{
private IContainer container;//not necessary..?
protected void Application_Start()
{
///etc..
this.container = builder.Build();//returns IDisposable instance
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
//etc..
}
protected void Application_End()
{
container.Dispose();
}
}
请你点我到正确的文档,如果我俯视着东西,谢谢!
这是不完全正确。如果AppDomain死亡,运行时将运行所有仍需要最终确定的对象的所有终结器。所以这意味着任何非托管资源(文件句柄,db连接,对本机内存的引用,GDI +映像)都会被清除。但垃圾收集器不会为您调用任何“Dispose”方法。因此,如果你在一个不在终结器中的dispose方法中做更多事情(例如对数据库进行一些最终的记录),那么代码将无法运行。 – Steven 2014-10-09 21:04:35
@Steven - 谢谢,你真是太棒了。我澄清了这一点。 – 2014-10-10 10:08:18