When you include .Net 2.0 mixed mode assemblies in .Net 4.0 or .Net 4.5 projects you often have to add some start up options to your project’s config file to get it all to play nice when your app starts up. This backwards compatibility feature is great as it allow you to use older/non supported projects in your recent work.When when using Visual Studio 2012’s new unit testing tools this magical piece of app.config code doesn’t seem to help though, and the solution is pretty simple.
Recently I’ve had great fun working on some green field .Net 4.5 projects. While Unit Testing some of the code in these projects using Visual Studio’s “super sweet” async testing tools I’ve come across a niggling little issue that breaks with normal convention when it comes to testing code that depends on older assemblies.
Normally when you are loading .Net 2.0 assemblies as a reference into a .Net 4.0 or 4.5 project you have to tell .Net that it should run in a compatibility mode so that your old assembly will load properly.
This is because .Net 2, 3.0 and 3.5 assemblies are all built against the .net 2.0 CLR.
.Net 4.0 and 4.5 assemblies are built against different CLR’s and therefore startup differently.
For backwards compatibility the Microsoft .Net teams kindly allowed you the add the following to your web.config or app.config to make this all still work:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> <supportedRuntime version="v2.0.50727"></supportedRuntime> </startup> </configuration>
The above will make your application run fine when placed into the app’s config file.
When writing unit tests against a .Net 4.0 or 4.5 assembly and running these unit tests inside Visual Studio 2012 this fix sadly doesn’t work.
Case in point with my SharpSVN “bang my head against a brick wall” exception below which worked when running in my app, but failed under unit test (the be fair this is an integration test – apologies to the unit testing elitists out there…):
The reason the normal app.config fix doesn’t work when using the Microsoft Test runner built into Visual Studio 2012 is that the test runner’s environment isn’t inside your test project’s bin folder. It’s running somewhere else.
The file you need to place your compatibility config settings in is:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.executionengine86.exe.config
If you open the above file using your favourite notepad (running as Administrator) and place the same “startup” section mentioned higher in this post inside this file, all your problems resolve themselves.
Hopefully the above post can save you the time it took me to find it.