应用程序重新启动时未处理 Autofac SingleInstance?

Autofac SingleInstance not disposed when app restarts?

在我的 MVC4 项目中,我在创建的对象中有一个 lucene IndexWriter,它是一个名为 LuceneIndex.
的 IDisposable 对象 当我的对象被处置时,它调用索引编写器的 Dispose() 方法。 处置 IndexWriter 会删除磁盘上名为 write.lock 的文件。 我的 LuceneIndex 是这样注册的:

builder.RegisterType<LuceneIndex>()
 .WithParameter("indexLocation", HttpContext.Current.Server.MapPath(UmbracoConfig.IndexPath))
 .SingleInstance();

当我 stop/recycle/restart 网站 write.lock 没有被删除时,因此从现在开始我从 lucene 中得到一个异常,如下所示:

Message: [LockObtainFailedException: Lock obtain timed out: NativeFSLock@C:\Dev\FOO\http\App_Data\Index\write.lock: System.IO.IOException: The process cannot access the file 'C:\Dev\FOO\http\App_Data\Index\write.lock' because it is being used by another process. ...

注意:dispose方法中的断点没有命中。应该吗?

确保正确处理 IndexWriter 以使其不留下 write.lock 的最佳方法是什么?

我应该做一些完全不同的事情吗?

必须在容器上显式调用 Dispose,如下所示:

protected override void OnApplicationEnd(object sender, EventArgs e)
{
    _container.Dispose();
    base.OnApplicationEnd(sender, e);
}

附带说明:应用程序结束和应用程序开始重叠,因此结束进程在新进程启动时仍然具有 write.lock 文件句柄。为了解决这个问题,我只是将 Lucene 锁获取超时 WRITE_LOCK_TIMEOUT 增加到 1 分钟来处理这些情况。