这个问题是关于在 ASP.Net MVC 应用程序的生命周期中创建 Raven 文档存储的单个实例,然后为每个请求使用新的 IDocumentSession 来为客户端提供服务。我相信已遵循http://ravendb.net/kb/3/using-ravendb-in-an-asp-net-mvc-website中描述的推荐程序,一旦部署站点,我仍然会遇到错误。
我的代码如下所示。HepApp 类包含文档存储并由应用程序引用:
internal class HepApp
{
internal static readonly Object padlock = new object();
private static IDocumentStore _DB;
internal IDocumentStore DB
{
get
{
if (_DB == null)
{
lock (padlock)
{
if (_DB == null)
{
_DB = new EmbeddableDocumentStore().Initialize();
return _DB;
}
}
}
return _DB;
}
}
public IDocumentSession GetSession()
{
return DB.OpenSession();
}
}
我的 MVC 控制器:
public class HomeController : Controller
{
IDocumentSession RavenSession;
public HomeController()
{
}
protected void Init()
{
RavenSession = MvcApplication.HepApp.GetSession();
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
Init();
}
应用程序类 Global.asax 绑定到:
public class MvcApplication : System.Web.HttpApplication
{
static readonly Object padlock = new object();
static HepApp _HepApp;
internal static HepApp HepApp
{
get
{
if(_HepApp == null)
{
lock(padlock)
{
if(_HepApp == null)
{
_HepApp = new HepApp();
}
}
}
return _HepApp;
}
}
当应用程序启动时,文档存储应该被初始化并在应用程序的剩余生命周期中存在。对于每个请求,都会创建一个新的 Session 对象。至少,是这样的想法。相反,在 OnActionExecuting 中发生空引用错误(尽管我怀疑 HomeController.Init 方法,但它没有出现在堆栈上)或 EmbeddableDocumentStore 对象被报告为已经处置。令人困惑的是,这些错误仅在应用程序运行一段时间后才会出现,这可能表明这是应用程序池回收问题
编辑:错误有所不同,但似乎都表明数据库存储的实例化存在问题:
[EsentFileAccessDeniedException:无法访问文件,文件已锁定或正在使用] [InvalidOperationException:无法写入位置:\192.168.0.100\localuser\x\App_Data/Raven。确保您对此路径具有读/写权限。]
System.InvalidOperationException:无法写入位置:\192.168.0.100\localuser\x\App_Data/Raven。确保您对此路径具有读/写权限。---> Microsoft.Isam.Esent.Interop.EsentFileAccessDeniedException:无法访问文件,文件在 Microsoft.Isam.Esent.Interop.Api.Check(Int32 err) 在 Microsoft.Isam.Esent.Interop 被锁定或正在使用。 Api.JetInit(JET_INSTANCE& 实例) 在 Raven.Storage.Esent.TransactionalStorage.Initialize(IUuidGenerator uuidGenerator, OrderedPartCollection
1 documentCodecs) --- End of inner exception stack trace --- at Raven.Storage.Esent.TransactionalStorage.Initialize(IUuidGenerator uuidGenerator, OrderedPartCollection
1 documentCodecs) 在 Raven.Database.DocumentDatabase..ctor(InMemoryRavenConfiguration 配置, TransportState transportState) 在 Raven.Client.Embedded.EmbeddableDocumentStore.InitializeInternal() 在 Raven.Client.Document.DocumentStore.Initialize() 在 HEPlaceHolder.HepApp.get_DB( ) 在 e:\projects\HEPlaceHolder\HEPlaceHolder\Logic\HepApp.cs:line 27 at HEPlaceHolder.Controllers.HomeController.OnActionExecuting(ActionExecutingContext filterContext) 在 e:\projects\HEPlaceHolder\HEPlaceHolder\Controllers\HomeController.cs:line 31 at System.Web.Mvc.Controller.System.Web.Mvc.IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) 在 System.Web.Mvc.Async。AsyncControllerActionInvoker.<>c__DisplayClass33.b__31(AsyncCallback asyncCallback, Object asyncState) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1. 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary
2 parameters, AsyncCallback callback, Object state) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker 开始(AsyncCallback 回调,对象状态,Int32 超时)。 <>c__DisplayClass21.b__19(AsyncCallback asyncCallback, Object asyncState) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.在 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) 在 System.Web.Mvc.Controller.b__1c(AsyncCallback) 开始(AsyncCallback 回调,对象状态,Int32 超时) asyncCallback,对象 asyncState,ExecuteCoreState innerState)在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.Begin(AsyncCallback 回调,对象状态,Int32 超时)在 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback 回调,对象状态)在 System.Web.Mvc.Controller.b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.在 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) 在 System.Web.Mvc.Controller.System.Web.Mvc.Async 开始(AsyncCallback 回调,对象状态,Int32 超时)。 IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.在 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback 回调, 对象状态) 在 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback 回调中的开始(AsyncCallback 回调, 对象状态, Int32 超时),对象状态)在 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)[ObjectDisposedException:文档存储已被释放,无法使用对象名称:'EmbeddableDocumentStore'。] Raven.Client.DocumentStoreBase.EnsureNotClosed() +82 System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback,对象状态,Int32 超时)+128
[NullReferenceException:对象引用未设置为对象的实例。] e:\projects\HEPlaceHolder\HEPlaceHolder\Controllers\HomeController.cs:31 System.Web.Mvc.Controller 中的 HEPlaceHolder.Controllers.HomeController.OnActionExecuting(ActionExecutingContext filterContext) .System.Web.Mvc.IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) +10
System.InvalidOperationException:无法打开事务存储:\192.168.0.100\localuser\x\App_Data/Raven\Data ---> Microsoft.Isam.Esent.Interop.EsentTempPathInUseException:临时路径已被 Microsoft.Isam 的另一个数据库实例使用.Esent.Interop.Api.Check(Int32 err) 在 Microsoft.Isam.Esent.Interop.Api.JetInit(JET_INSTANCE& 实例) 在 Raven.Storage.Esent.TransactionalStorage.Initialize(IUuidGenerator uuidGenerator, OrderedPartCollection
1 documentCodecs) --- End of inner exception stack trace --- at Raven.Storage.Esent.TransactionalStorage.Initialize(IUuidGenerator uuidGenerator, OrderedPartCollection
1 documentCodecs) 在 Raven.Database.DocumentDatabase..ctor(InMemoryRavenConfiguration 配置, TransportState transportState) 在 Raven.Client.Embedded.EmbeddableDocumentStore.InitializeInternal() 在 Raven.Client.Document.DocumentStore.Initialize() 在 HEPlaceHolder.HepApp.get_DB( ) 在 e:\projects\HEPlaceHolder\HEPlaceHolder\Logic\HepApp.cs:line 27 at HEPlaceHolder.Controllers.HomeController.OnActionExecuting(ActionExecutingContext filterContext) 在 e:\projects\HEPlaceHolder\HEPlaceHolder\Controllers\HomeController.cs:line 31 at System.Web.Mvc.Controller.System.Web.Mvc.IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) 在 System.Web.Mvc.Async。AsyncControllerActionInvoker.<>c__DisplayClass33.b__31(AsyncCallback asyncCallback, Object asyncState) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1. 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary
2 parameters, AsyncCallback callback, Object state) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker 开始(AsyncCallback 回调,对象状态,Int32 超时)。 <>c__DisplayClass21.b__19(AsyncCallback asyncCallback, Object asyncState) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.在 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) 在 System.Web.Mvc.Controller.b__1c(AsyncCallback) 开始(AsyncCallback 回调,对象状态,Int32 超时) asyncCallback,对象 asyncState,ExecuteCoreState innerState)在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.Begin(AsyncCallback 回调,对象状态,Int32 超时)在 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback 回调,对象状态)在 System.Web.Mvc.Controller.b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.在 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) 在 System.Web.Mvc.Controller.System.Web.Mvc.Async 开始(AsyncCallback 回调,对象状态,Int32 超时)。 IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.在 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback 回调, 对象状态) 在 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback 回调中的开始(AsyncCallback 回调, 对象状态, Int32 超时),对象状态)在 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
编辑:每当 bin 文件夹使用经过调整的版本进行更新以调试问题时,该站点最初会正常运行,可能是因为应用程序已重置。