Forcing a .NET application to use a 4.0 CLR

Posted on Wed 14 December 2011 in Coding

I was in a situation where I had an application built with .NET 3.5, but the application needed to launch a sub system build with .NET 4.0. This didn’t work, obviously, as the 3.5 CLR refused to load the 4.0 assemblies. My first idea was to create some sort of COM bridge, which turned out to exist already. Luckily, I couldn’t get the bridge to work when the launching assembly was a Windows Forms Application. I say luckily, as I then encountered a much, much simpler solution.

The key is In-Process Side-by-Side Execution, which means that several CLR versions can be hosted within the same process. Thus, all I had to do was to add the supportedRuntime element to the app.config file of the main application:

<configuration>
   <startup>
      <supportedRuntime version="v4.0"/>
   </startup>
</configuration>

Without that element, the application would use the CLR for which it was built, which was 3.5. With the element, it uses the 4.0 CLR and happily loads the sub system assemblies built with .NET 4.0.