Wednesday, February 10, 2010

Using .NET 3.5 (CLR 2.0) DLL inside Visual Studio 2010 for a .NET 4.0 Project/Application

When you first create a .NET 4.0 project inside VS 2010 and add reference to a .NET 3.5 DLL (say log4net or Rhino.Mocks), the project tends to not build. You can get errors as shown below.

image

It might appear to be a CLR version issue – unable to run 2.0 DLL inside a 4.0 App Domain. And i thought that is what it was until now.

You can get over this :)

By default, VS 2010 creates the projects with Target Framework on the properties set to “.NET Framework 4 Client Profile”. You would have to change that to “.NET Framework 4” by going to Project Properties –> Application –>  Target Framework. And every thing begins to compile.

So I guess, one has to be aware of this when migrating old solutions from Visual Studio 2008 to Visual Studio 2010.

Proof that it works :) Notice the .NET Framework 4.0 features as well Log4Net and Rhino.Mocks used in all the same example. (It is a stupid example, but the intention was to show it works).

image

If for some reason, it does not work for you, try to add

<?xml version ="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
     <supportedRuntime version="v2.0.5727" /> 
    <supportedRuntime version="v4.0.21006"/>
  </startup>
</configuration>

to your csc.exe.config/msbuild.exe.config/VCSExpress.exe.config/devenv.exe.config …

I initially thought it has something to do with not enabling Side-By-Side Execution of the  compiler and stuff but it turns out that it is not the case. For your information,  i have added supported runtime as .NET 2.0 but then commented it to be sure that its ONLY the Target Framework that has to be changed.

2 comments:

Unknown said...

Thanks ..Your solution worked

Selva said...

Great! It worked