0

エリアルーティングの一部をテストするためにASP.NETMVCアプリケーションに単体テストを追加した瞬間、次のスタックトレースで型初期化子がHttpException出てきました。System.Web.Complication.CompilationLock

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()
4

1 に答える 1

2

MSDNのMVCAreasチュートリアルに従った他の人にとって、作成したWebアプリケーションに単体テストを追加すると問題が発生します。

メソッドに追加するように指示しAreaRegistration.RegisterAllAreas()ますRegisterRoutes。残念ながら、それは単体テストから呼び出されたときに動揺する静的メソッドです。

代わりに、変更したばかりの呼び出しの直前Application_Startに、内の領域を登録します。RegisterRoutes最初に呼び出すとRegisterRoutes、UrlParameter.Optionalはエリアルートでの動作を停止しているように見えます(ただし、非エリアルートでは動作し続けます)。

protected void Application_Start() {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
}
于 2010-05-17T18:06:05.530 に答える