Saturday, 5 May 2012

Encountering “System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.” inside a Test Project in Visual Studio 11 beta?

Even though you may have configured the app.config file of the Test Project with the following configuration, it’ll make no difference when trying to use .NET 2.0 assemblies from a .NET 4 Test project.

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

This configuration node is not effective because Test projects are DLLs not executables. This node will not effect the actual underlying executable which hosts the Test Project’s DLL when you run tests.  You need to change the config of the executable which for me is hosted under: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow.

Look for “vstest.executionengine.x86.exe”.  This is the executable which VS runs while unit testing. Now find “vstest.executionengine.x86.exe.config”

Add the above configuration node to this file, save it, run your tests and now you should find the above exception disappears. 

The actual exception for me occurred while loading a Windows Azure SDK DLL:

   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_IsAvailable()

Hopefully Microsoft will fix this issue in time for RTM.