2011-03-21 26 views
19

將ASP.NET MVC 3.0與Visual Studio 2010(SP1以前版本和SP1)以及ASP.NET Development服務器一起使用出現錯誤「_AppStart爲」時無法創建存儲範圍每次我調試時都會執行「」。當我等待幾秒鐘並刷新瀏覽器時,它按預期工作。當_AppStart執行時無法創建存儲範圍

  • 這是否發生在每個人身上?
  • 有沒有辦法阻止它?
  • 我應該在生產中關注這個嗎? (IIS 6/7託管)

相關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 
+0

你與IIS快遞(運行時,與VS 2010 SP1可以解決),而不是Web開發工作室? – Linkgoron 2011-03-21 21:52:43

+0

@Linkgoron我剛剛使用VS2010 SP1和WebDevServer(不是IIS Express)創建了一個新的MVC3項目,以驗證MVC解決方案模板中是否存在該錯誤。然後我將服務器切換到IIS Express,清理解決方案以確保它是全新的構建,並且錯誤**沒有發生。 – recalde 2011-03-22 14:49:39

+0

那麼,正如我在我的較長文章中所說的,我的* bet *是這是PreApplicationStart + WebDev服務器+動態HttpModule註冊的問題。無論哪種方式,似乎這起源於Webdev的服務器,所以生產安全... – Linkgoron 2011-03-22 15:00:37

回答

7

雖然我不知道爲什麼會這樣(我也無法重現此),我可能有一些修正,你可以嘗試。 WebPageHttpModule(在System.Web.WebPages中)假設設置爲AppStartExecuteCompleted,但它沒有,這就是爲什麼你會得到一個異常(所有這些猜測工作都是基於此)。

所以......環顧源代碼,我相信這個錯誤的根源是有鬼用模塊(WebPageHttpModule)的Web開發服務器和PreApplicationCode +動態註冊。模塊初始化應始終在應用程序啓動時運行,但由於某些原因,我並不知道它不會。也許有人可以以某種方式測試它。

我想過的一些修復(再次,這是純粹的猜測工作,因爲我不能再現這一點)。

1)嘗試在WebConfig中靜態註冊WebPageHttpModule模塊。 更新我測試過了,這不會引發異常。但是,它註冊了兩個httpmodules。

2)根據該鏈接上的評論,你已經鏈接它似乎不會發生在IIS上,所以我會嘗試在IIS Express下運行(在VS 2010 SP1下)。

正如我之前所說,這只是猜測工作。可能還有其他原因導致模塊無法運行...比如Web服務器沒有正確重啓,包括PreApplicationCode的一些問題,或者沒有將HttpModule中的s_appStartExecutedLock重置爲false,或者使用HttpContext.Items也可能會更糟導致模塊不運行...

2

我剛剛在我的項目上第一次發生這種情況......也無法重現。但我有一個理論:

在思考過程中,我已經在助手類中聲明瞭一個靜態字典對象,並且在重新編譯之後不久,我得到了這個錯誤。這是我的聲明:

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

由於靜態變量在整個Web應用程序(而不僅僅是要求的上下文中)共享,我想同時App_Start正在執行的字典構造方法中得到觸發。這對我來說很有意義......因爲App_Start可能會設置所有會話和應用程序存儲。如果一個靜態變量在存儲建立之前就被構建出來了......繁榮就是炸藥。

只是想拋出那個理論......也許這是原因......也許它不是。但這是值得研究的東西(因爲靜態聲明的對象很危險。)

-1

斷開數據庫。連接處於活動狀態時不能重新創建。

謝謝。

3

這似乎仍然是一個問題,當然在VS 2010 SP1上使用MVC 3模板。對於任何人仍然有問題,我發現了以下博客文章的解決方案能夠快速,可靠地工作:

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

+1

感謝您的鏈接。 – 2013-12-22 18:45:06

+0

此鏈接解決了我的問題。謝謝。 – Spencer 2014-03-16 01:46:39

+0

不要這樣做!鏈接提供了有關如何使用反射來查找內部靜態'AppStartExecuteCompleted'屬性和值更改爲true的指令。這非常糟糕,因爲它以原始開發人員意想不到的方式更改了值,並可能導致意外問題。 – Trisped 2015-06-30 18:20:09

-2

簡單的重建解決方案在幫助我,請嘗試清理和重建(不知道「微軟」發佈MVC3任何重錯誤)