Using ASP.NET MVC 3.0 with Visual Studio 2010 (Pre-SP1 and with SP1) and ASP.NET Development server I get the error “Storage scopes cannot be created when _AppStart is executing.” every time I debug. When I wait a couple seconds and refresh the browser it works as expected.
- Does this happen to everyone?
- Is there a way to prevent it?
- Should I be concerned about this in production? (IIS 6/7 hosting)
Related work-item #7828 on codeplex has no official responses.
Full stack trace:
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
While I’m not sure why this happens (I also can’t reproduce this) I might have some fixes that you might try. The WebPageHttpModule (in System.Web.WebPages) is supposed set AppStartExecuteCompleted but it doesn’t, and that’s why you get an exception (All this guess work is based on that).
So… Looking around the source code I BELIEVE that the source of this bug is something fishy with the Web Development Server and PreApplicationCode + dynamic registration of a module (WebPageHttpModule). The Module init should ALWAYS run at the application start, but for some reason I GUESS that it doesn’t. Maybe someone can somehow test it.
Some fixes that I thought about (again, this is pure guess work as I can not reproduce this).
1) try to register the WebPageHttpModule module statically in the WebConfig. update I’ve tested this, and this doesn’t throw an exception. However, it registers TWO httpmodules.
2) According to the comments on that thread you’ve linked it appears that this doesn’t happen on IIS, so I would try to run under IIS Express (under VS 2010 SP1).
As I said before, this is just guess work. There could be other reasons for module not to run… Such as the Web Server not restarting properly, including some problem with the PreApplicationCode, or not resetting s_appStartExecutedLock to false in the HttpModule, or something wierd with the HttpContext.Items which could ALSO cause the module not to run…