Problems with .Net 2.0 Mixed Mode Assemblies inside Visual Studio .Net 4.5 Test Projects
Join the DZone community and get the full member experience.Join For Free
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.
Published at DZone with permission of Douglas Rathbone, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.