3

What is the difference between the three following scenarios (in an app.config files of an exe)?

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


<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" />
  <supportedRuntime version="v2.0" />
</startup>


<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" />
</startup>

I've read the MS documentation on it and some blogs but it still isn't very clear to me exactly what happens and when to use which.

EDIT
I have a situation where a third party application was compiled with CLR 2 (and also uses legacy COM) and the allowed extensions that I've made for the application are compiled with CLR 4. So, recompiling the application is not an option for me. I just need to know the impact of the three scenarios.

bsara
  • 7,350
  • 3
  • 26
  • 46
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 06 '13 at 18:33
  • the third scenario is, indeed, the best one. It solves the problem effectively. Nicely done. This affected something I had in production, so the solution helped me make the quick fix. – Scott Shaw-Smith Jun 02 '15 at 16:17

2 Answers2

4

The useLegacyV2RuntimeActivationPolicy attribute is a bit of a cop-out. Setting it to true allows a .NET 4 program to load mixed-mode (C++/CLI) or [ComVisible] .NET assemblies that stated expressly in the registry that they need version 2.0.50727 of the runtime. It won't make any difference if you don't have such assemblies, they are fairly rare. The sane thing to do is to not use it, you'll get an error message when it is required. A FileLoadException whose message looks like:

Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

The next sane thing to do is rebuild such assemblies to target .NET 4. Last thing you do is use that attribute.

If you offer more than one version of the CLR, like you did in the first two snippets, then you'll get the one that the EXE asks for in its manifest. The last one forces the v4 version. The implication is that you'll potentially run code that was only ever tested on CLR v2 on a different .NET runtime. This will almost always come to a good end, v4 is very compatible with v2. But they did take the opportunity to fix bugs in v4. You could be accidentally depend on buggy behavior. Very rare of course.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • The assemblies don't need to be `ComVisible` - this is also necessary if loading C++/CLI assemblies targeting .NET 3.5, for example... – Reed Copsey May 06 '13 at 18:26
  • On example is the `Microsoft.SqlServer.BatchParser.dll` from/for the SQL Server 2008 R2 product. It is a mixed mode assembly compiled against .NET 2.0 and you need the `useLegacyV2RuntimeActivationPolicy` set to `true` to use from a .NET 4.0 assembly. – Christian.K May 07 '13 at 05:30
2

supportedRuntime specifies which runtime is used to actually run and execute the application itself. If you set this to v4.0, then the 4.0 clr runtime will be used to start the application. Adding v2.0 and v4.0 is saying that you'll allow the CLR 2 or 4 runtime to actually run the application.

The useLegacyV2RuntimeActivationPolicy option changes the behavior when you load an assembly targeting the CLR 2 runtime from within an application executing in the CLR 4 runtime. When it is set to true, the 4.0 runtime will be used to load the CLR 2 assembly. This is mainly required if you're loading a mixed mode assembly targeting CLR 2 within a CLR 4 project, as you'll get an error otherwise.

Reed Copsey
  • 522,342
  • 70
  • 1,092
  • 1,340