开发者

ReflectionTypeLoadException for Microsoft.Web.Administration running Test Project in VS2010

开发者 https://www.devze.com 2023-01-10 12:13 出处:网络
We have recently upgraded all our VS2008 projects to VS2010.Our code base is still targeting framew开发者_StackOverflow社区ork version 3.5, but I\'m sure most of you would know that test projects must

We have recently upgraded all our VS2008 projects to VS2010. Our code base is still targeting framew开发者_StackOverflow社区ork version 3.5, but I'm sure most of you would know that test projects must be upgraded to framework version 4.0.

We have one particular set of tests that do not work now that the test project is targeting framework 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". If I then call

GetTypes()

on this assembly I get a ReflectionTypeLoadException saying it can't load assembly "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. I have done some reading and realise the Administration dll is part of IIS7. I am developing on XP and do not have IIS7 installed.

My real question is - why is Microsoft.VisualStudio.Enterprise.AspNetHelper in my app domain in VS2010 tests but not in VS2008 tests? 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:

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

I think the TODO says it all... but I haven't found a better way yet :-)


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. 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.

If I substitute GetExportedTypes() for GetTypes() I am able to work around this. This is an easy solution if you don't need internal types.

0

精彩评论

暂无评论...
验证码 换一张
取 消