Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storage scopes cannot be created when _AppStart is executing

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
like image 566
recalde Avatar asked Mar 21 '11 19:03

recalde


3 Answers

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...

like image 159
Linkgoron Avatar answered Nov 06 '22 20:11

Linkgoron


This seems to still be a problem, certainly using the MVC 3 template on VS 2010 SP1. For anyone still having the problem, I found the solution on the following blog post to work quickly and reliably:

http://www.codinghub.net/2012/12/storage-scopes-cannot-be-created-when.html

like image 25
Wayne Koorts Avatar answered Nov 06 '22 18:11

Wayne Koorts


I just had this happen for the first time on my project...and also can't reproduce. But I have a theory:

In a lapse of thinking, I had declared a static dictionary object within a helper class, and not too long after recompiling I got this error. This is my declaration:

private static Dictionary<string, string> FormatList = new Dictionary<string, string>();

Since static variables are shared across the entire web app (not just the context of the request), I'm thinking the constructor for the dictionary was getting triggered while App_Start was executing. This makes sense to me...since App_Start is probably setting up all of the session and application storage. If a static variable gets constructed before the storage is set up...boom goes the dynamite.

Just wanted to toss that theory out there...maybe it's the cause...maybe it's not. But it's something worth looking into (since statically declared objects like that are dangerous.)

like image 2
Joe the Coder Avatar answered Nov 06 '22 18:11

Joe the Coder