CompliationLock抛出HttpException注册ASP.NET MVC的单元测试时区 [英] CompliationLock throws HttpException when registering areas for ASP.NET MVC unit tests

查看:132
本文介绍了CompliationLock抛出HttpException注册ASP.NET MVC的单元测试时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个单元测试到我的ASP.NET MVC应用程序的时间来测试一些区域的路由,我得到了一个 HttpException 走出<$ C $的C> System.Web.Complication.CompilationLock 与下面的堆栈跟踪类型的初始化。

  System.Web.HttpException:对System.Web.Compilation.CompilationLock'的类型初始值引发了异常。
  ----&GT; System.TypeInitializationException:对于System.Web.Compilation.CompilationLock'的类型初始值引发了异常。
  ----&GT; System.NullReferenceException:对象不设置到对象的实例。
在System.Web.Compilation.BuildManager.ReportTopLevelCompilationException()
在System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
在System.Web.Compilation.BuildManager.GetReferencedAssemblies()
在System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.GetReferencedAssemblies()
在System.Web.Mvc.TypeCacheUtil.FilterTypesInAssemblies(IBuildManager buildManager,predicate`1 predicate)
在System.Web.Mvc.TypeCacheUtil.GetFilteredTypesFromAssemblies(字符串cacheName,predicate`1 predicate,IBuildManager buildManager)
在System.Web.Mvc.AreaRegistration.RegisterAllAreas(RouteCollection路线,IBuildManager buildManager,对象状态)
在System.Web.Mvc.AreaRegistration.RegisterAllAreas(对象状态)
在System.Web.Mvc.AreaRegistration.RegisterAllAreas()
在Global.asax.cs中StpWeb.MvcApplication.RegisterRoutes(RouteCollection路线):第16行
在StpWeb.Tests.RoutesTest.TestFixtureSetUp()在RoutesTest.cs:11行
--TypeInitializationException
在System.Web.Compilation.CompilationLock.GetLock(REF布尔gotLock)
在System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
--NullReferenceException
在System.Web.Compilation.CompilationLock..cctor()


解决方案

有关其他人随后的MVC的相对=nofollow>领域的教程,你会发现一个问题,如果你曾经把单元测试添加到您创建的Web应用程序。

它会告诉你添加 AreaRegistration.RegisterAllAreas()的RegisterRoutes 方法。不幸的是,从单元测试调用时变得心烦意乱的静态方法。

相反,注册在的Application_Start 区域,右侧前<$ ​​C $ C>的RegisterRoutes 呼叫你刚修改。如果你调用的RegisterRoutes 第一,UrlParameter.Optional似乎停止在区域内路由的工作(保持在非地区航线的工作,虽然)。

 保护无效的Application_Start(){
        AreaRegistration.RegisterAllAreas();
        的RegisterRoutes(RouteTable.Routes);
}

The moment I added a unit test to my ASP.NET MVC application to test some of the area routing, I got an HttpException coming out of the System.Web.Complication.CompilationLock type initializer with the following stack trace.

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

For anyone else that followed the MVC Areas tutorials on MSDN, you will find an issue if you ever add unit tests to the web application you created.

It tells you to add AreaRegistration.RegisterAllAreas() to the RegisterRoutes method. Unfortunately, that is a static method that gets upset when called from unit tests.

Instead, register areas within Application_Start, right before the RegisterRoutes call you just modified. If you call RegisterRoutes first, UrlParameter.Optional appears to stop working in area routes (keeps working in non-area routes, though).

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

这篇关于CompliationLock抛出HttpException注册ASP.NET MVC的单元测试时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆