将 ASP.NET MVC 3.0 与 Visual Studio 2010(Pre-SP1 和 SP1)和 ASP.NET 开发服务器一起使用,我每次调试时都会收到错误“ _AppStart 执行时无法创建存储范围。” .当我等待几秒钟并刷新浏览器时,它会按预期工作。
相关 work-item #7828 on codeplex没有官方回应。
完整的堆栈跟踪:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Storage scopes cannot be created when _AppStart is executing.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Storage scopes cannot be created when _AppStart is executing.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidOperationException: Storage scopes cannot be created when _AppStart is executing.]
System.Web.WebPages.Scope.AspNetRequestScopeStorageProvider.set_CurrentScope(IDictionary`2 value) +83361
System.Web.WebPages.Scope.ScopeStorage.CreateTransientScope(IDictionary`2 context) +95
System.Web.WebPages.Scope.ScopeStorage.CreateTransientScope() +76
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +84
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
最佳答案
虽然我不确定为什么会发生这种情况(我也无法重现),但我可能有一些您可以尝试的修复程序。 WebPageHttpModule(在 System.Web.WebPages 中)应该设置 AppStartExecuteCompleted 但它没有,这就是为什么你会得到一个异常(所有这些猜测工作都基于此)。
所以...查看源代码我相信这个错误的来源是 Web 开发服务器和 PreApplicationCode + 模块的动态注册(WebPageHttpModule)的一些可疑之处。 Module init 应该始终在应用程序启动时运行,但由于某种原因,我猜它不会。也许有人可以以某种方式对其进行测试。
我考虑过的一些修复(同样,这纯粹是猜测工作,因为我无法重现)。
1)尝试在WebConfig中静态注册WebPageHttpModule模块。更新我已经测试过了,这不会引发异常。但是,它注册了两个 httpmodules。
2)根据您链接的该线程的评论,这似乎不会在 IIS 上发生,所以我会尝试在 IIS Express 下运行(在 VS 2010 SP1 下)。
正如我之前所说,这只是猜测工作。模块不运行可能还有其他原因......例如 Web 服务器没有正确重新启动,包括 PreApplicationCode 的一些问题,或者没有在 HttpModule 中将 s_appStartExecutedLock 重置为 false,或者 HttpContext.Items 的一些奇怪的东西也可能导致模块不运行...
关于asp.net-mvc-3 - _AppStart 执行时无法创建存储范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5382856/