2010-05-17 36 views
0

當我向ASP.NET MVC應用程序添加一個單元測試以測試某些區域路由時,我得到了一個來自System.Web.Complication.CompilationLock類型初始值設定項的HttpException以下堆棧跟蹤。CompliationLock在爲ASP.NET MVC單元測試註冊區域時拋出HttpException

System.Web.HttpException : The type initializer for 'System.Web.Compilation.CompilationLock' threw an exception. 
    ----> System.TypeInitializationException : The type initializer for 'System.Web.Compilation.CompilationLock' threw an exception. 
    ----> System.NullReferenceException : Object reference not set to an instance of an object. 
at System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() 
at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() 
at System.Web.Compilation.BuildManager.GetReferencedAssemblies() 
at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.GetReferencedAssemblies() 
at System.Web.Mvc.TypeCacheUtil.FilterTypesInAssemblies(IBuildManager buildManager, Predicate`1 predicate) 
at System.Web.Mvc.TypeCacheUtil.GetFilteredTypesFromAssemblies(String cacheName, Predicate`1 predicate, IBuildManager buildManager) 
at System.Web.Mvc.AreaRegistration.RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, Object state) 
at System.Web.Mvc.AreaRegistration.RegisterAllAreas(Object state) 
at System.Web.Mvc.AreaRegistration.RegisterAllAreas() 
at StpWeb.MvcApplication.RegisterRoutes(RouteCollection routes) in Global.asax.cs: line 16 
at StpWeb.Tests.RoutesTest.TestFixtureSetUp() in RoutesTest.cs: line 11 
--TypeInitializationException 
at System.Web.Compilation.CompilationLock.GetLock(ref Boolean gotLock) 
at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() 
--NullReferenceException 
at System.Web.Compilation.CompilationLock..cctor() 

回答

2

爲別人跟隨的MVC Areas tutorials on MSDN,你會發現一個問題,如果你曾經把單元測試添加到您創建的Web應用程序。

它告訴您將AreaRegistration.RegisterAllAreas()添加到RegisterRoutes方法。不幸的是,這是一種靜態方法,當從單元測試中調用時會變得不安。

取而代之的是,在Application_Start之內註冊區域,就在您剛修改RegisterRoutes之前。如果您先撥打RegisterRoutes,UrlParameter.Optional似乎停止在區域路由中工作(儘管在非區域路由中工作)。

protected void Application_Start() { 
     AreaRegistration.RegisterAllAreas(); 
     RegisterRoutes(RouteTable.Routes); 
} 
+0

我可以用同樣的問題在MVC的CodePlex網站已經保存了幾個小時,如果我發現[這個人](http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=4915) 。對該問題的評論提供了相同的解決方案。 – patridge 2010-05-17 18:36:24

+0

當您創建一個空的MVC2項目時,這似乎是在VS2010中的Global.asax.cs中默認設置的。 – patridge 2010-09-19 03:18:46

相關問題