简体   繁体   English

在VS2010中运行测试项目的Microsoft.Web.Administration的ReflectionTypeLoadException

[英]ReflectionTypeLoadException for Microsoft.Web.Administration running Test Project in VS2010

We have recently upgraded all our VS2008 projects to VS2010. 我们最近已将所有VS2008项目升级到VS2010。 Our code base is still targeting framework version 3.5, but I'm sure most of you would know that test projects must be upgraded to framework version 4.0. 我们的代码库仍然针对框架版本3.5,但是我敢肯定你们大多数人都知道测试项目必须升级到框架版本4.0。

We have one particular set of tests that do not work now that the test project is targeting framework 4.0. 由于测试项目针对框架4.0,因此我们有一组无法使用的特定测试。 These tests all test code that is doing some sort of reflection task. 这些测试将测试正在执行某种反射任务的所有代码。 Through a bit of debugging I managed to narrow the problem down. 通过一些调试,我设法缩小了范围。

For some reason in the upgraded test project the following code: 由于某种原因,在升级的测试项目中,以下代码:

AppDomain.CurrentDomain.GetAssemblies();

will return a reference to "Microsoft.VisualStudio.Enterprise.AspNetHelper". 将返回对“ Microsoft.VisualStudio.Enterprise.AspNetHelper”的引用。 If I then call 如果我再打电话

GetTypes()

on this assembly I get a ReflectionTypeLoadException saying it can't load assembly "Microsoft.Web.Administration". 在此程序集上,我收到了ReflectionTypeLoadException,说它无法加载程序集“ Microsoft.Web.Administration”。

So it seems to me that there is some type within "Microsoft.VisualStudio.Enterprise.AspNetHelper" that inherits from or has some reference to another type in Microsoft.Web.Administration. 因此在我看来,“ Microsoft.VisualStudio.Enterprise.AspNetHelper”中存在某种类型,该类型继承或引用了Microsoft.Web.Administration中的另一种类型。 I have done some reading and realise the Administration dll is part of IIS7. 我已经阅读了一些书,并意识到Administration dll是IIS7的一部分。 I am developing on XP and do not have IIS7 installed. 我在XP上进行开发,没有安装IIS7。

My real question is - why is Microsoft.VisualStudio.Enterprise.AspNetHelper in my app domain in VS2010 tests but not in VS2008 tests? 我的真正问题是-为什么在我的应用程序域中的Microsoft.VisualStudio.Enterprise.AspNetHelper在VS2010测试中而不在VS2008测试中? Creating a simple console app that does the same thing does not seem to be a problem - only with test projects. 创建一个简单的控制台应用程序执行相同的操作似乎没有问题-仅对于测试项目。 How do I get around this? 我该如何解决?

It's a bit hacky but does the trick: 这有点hacky,但是可以解决这个问题:

AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a))

where 哪里

private bool IsIgnoredAssembly(Assembly assembly)
{
    // TODO - find a better way to remove "system" assemblies from the auto registration
    var ignoreChecks = new List<Func<Assembly, bool>>()
    {
        asm => asm.FullName.StartsWith("Microsoft.", StringComparison.InvariantCulture),
        asm => asm.FullName.StartsWith("System.", StringComparison.InvariantCulture),
        asm => asm.FullName.StartsWith("System,", StringComparison.InvariantCulture),
        asm => asm.FullName.StartsWith("CR_ExtUnitTest", StringComparison.InvariantCulture),
        asm => asm.FullName.StartsWith("mscorlib,", StringComparison.InvariantCulture),
        asm => asm.FullName.StartsWith("CR_VSTest", StringComparison.InvariantCulture),
        asm => asm.FullName.StartsWith("DevExpress.CodeRush", StringComparison.InvariantCulture),
        asm => asm.FullName.StartsWith("IronPython", StringComparison.InvariantCulture),
        asm => asm.FullName.StartsWith("IronRuby", StringComparison.InvariantCulture),
    };

    foreach (var check in ignoreChecks)
    {
        if (check(assembly))
            return true;
    }

    return false;
}

(Taken from TinyIoC - all credit to Steven Robbins.) (摘自TinyIoC-所有版权归史蒂芬·罗宾斯所有。)

I think the TODO says it all... but I haven't found a better way yet :-) 我认为TODO已经说明了一切...但是我还没有找到更好的方法:-)

I have the same problem with some tests running on a Win7 test lab machine, but I cannot repro on my Server 2008 R2 workstation. 我在Win7测试实验室计算机上运行某些测试时遇到相同的问题,但是我无法在Server 2008 R2工作站上进行复制。 I don't have the IIS server role enabled, but Microsoft.Web.Administration is in my GAC - I think this is why tests pass locally for me. 我没有启用IIS服务器角色,但是Microsoft.Web.Administration在我的GAC中-我认为这就是为什么测试在我本地通过的原因。

If I substitute GetExportedTypes() for GetTypes() I am able to work around this. 如果我用GetExportedTypes()代替GetTypes(),则可以解决此问题。 This is an easy solution if you don't need internal types. 如果不需要内部类型,这是一个简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM