views:

283

answers:

2

I'm trying to load a mixed managed application compiled and targeted for Framework 3.5 in the 4.0 CLR.

We have a .config file next to the .exe where I've added this node:

<?xml version="1.0"?> 
<configuration>
<startup>
    <supportedRuntime version="v4.0.21006" />
</startup>

Unfortunately, the app crashes out on startup with a nasty callstack. Can someone in the know confirm that a mixed managed app (.exe is C++/CLI) will not load in 4.0 if it was compiled for 3.5?

I'm watching a Channel9 video about side-by-side CLR hosting, and one of the devs seems to imply that this is the case:

http://channel9.msdn.com/shows/Going+Deep/CLR-4-Side-by-Side-In-Process-What-How-Why/

Thanks!

A: 

You should be able to run 3.5 and 4.0 in the same process. However forcing the app to use 4.0 instead of 3.5 doesn't look possible.

What are you trying to accomplish?

Ben Voigt
CLR 4 does allow CLR 2 (.NET 3.5) and CLR 4 (.NET 4) applications to be run in the same process. Mixed mode assemblies require special care, however.
Reed Copsey
I'd just love to be able to try our application on .NET 4.0 without having to modify a lot of .vcproj and .csproj files!In an ideal world, I could see us enabling .NET 4.0 in our app post-release by simply updating the .config file - and assuming nothing went wrong. But at this point I'm not even able to load the app at all, even if I 'opt-in'. Just wondering if anyone else had any luck.
cunningdave
@Reed: Having a .NET 3.5 application, you can now load plugins that use 4. Or you can load plugins requiring both versions into the same native app. But that's definitely not the same as forcing any code to run under a different version of the CLR than it was written for, in fact it's the exact opposite.
Ben Voigt
@Ben: Read up on useLegacyV2RuntimeActivationPolicy. This causes it to use the non-standard in clr4 policy, explicitly using the CLR 4 to load CLR2 assemblies (including mixed mode), which is required to use mixed mode assemblies in CLR 4.
Reed Copsey
I'm glad you were able to help the poster. It just seemed to me that the entire point of supporting side-by-side was to avoid loading an assembly using a newer CLR version. And as a result I wouldn't expect great effort making CLR4 completely compatible with CLR2. So as long as it works, great, but if you run into weird failures, try using the CLR version the assembly was designed for, because that won't block use of the new CLR in addition.
Ben Voigt
+2  A: 

You need to set useLegacyV2RuntimeActivationPolicy if you want to load a CLR 2 (.NET 3.5) mixed mode assembly in a CLR 4 process:

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

Without this, you'll get a nasty exception.

Reed Copsey
Awesome, this was my problem and this works for me. Thanks!
cunningdave