4

This might be a stupid question but here goes... I have a .NET exe which uses another .NET assembly. If the exe targets .NET 4.0 and the assembly targets .NET 3.5, when I run the exe does the assembly actually run on the .NET 4.0 framework? I suspect it does. My question then is is it possible to have the exe run on the 4.0 framework, but use an assembly which runs on the 3.5 framework?

Mark J
  • 41
  • 5
  • 1
    Yes, that is possible. The other way around is not possible. Notice that both managed executables and DLLs are assemblies. – Marius Bancila Apr 03 '14 at 12:55
  • That's great thanks, how do I get this to happen? – Mark J Apr 03 '14 at 13:02
  • 1
    If the assembly is mixed-mode, then you need to use the `useLegacyV2RuntimeActivationPolicy` setting in your config file (see http://stackoverflow.com/questions/1604663/what-does-uselegacyv2runtimeactivationpolicy-do-in-the-net-4-config). Otherwise you don't have to do anything. – Marius Bancila Apr 03 '14 at 13:05
  • See [Loading multiple CLR Runtimes](http://blogs.msdn.com/b/carlos/archive/2013/08/23/loading-multiple-clr-runtimes-inproc-sxs.aspx) for some discussion. Short answer: you'll only get one CLR in your process if you're just writing .NET code. You'd need some native code somewhere to actually get multiple CLRs in a single process. – Damien_The_Unbeliever Apr 03 '14 at 13:17
  • I misunderstood. You want both runtimes in your process? 4.0 for your 4.0 assemblies and 3.5 for your 3.5 assemblies? No, they all run under the same runtime, 4.0. However, hosting two runtimes inside the same process is possible starting with .NET 4.0 with in-process side-by-side, but not in a pure managed solution. http://msdn.microsoft.com/en-us/magazine/ee819091.aspx – Marius Bancila Apr 03 '14 at 13:22
  • Thanks for your answers guys. Ok what I think I'm after is not a different version of the CLR runtime, but different versions of the framework library. Basically there is a change in this between 3.5 and 4.0 I want to avoid, I want an exe using 4.0 but it use an assembly using 3.5. – Mark J Apr 03 '14 at 13:29

2 Answers2

5

No, that is not possible. Basic classes were changed in .NET 4.0, like System.String. Your .NET 3.5 assembly will say that it has a dependency on mscorlib v2.0.0.0. But the CLR will remap that and it will actually get the 4.0.0.0 version of the assembly. So every assembly in the process will agree what a string object looks like. Very important of course :)

.NET 4.0 does support in-process side-by-side CLR versioning, a process can have more than one CLR version loaded. But that will not happen in your case, it was meant to solve a problem when an unmanaged process loads .NET code. Like a COM server. It runs isolated without being aware that any other .NET code is present in the process. So can afford a different CLR version. Nothing like your case.

.NET 4.0 is very compatible with 3.5 and is pretty unlikely that the code in that 3.5 assembly will fail. It is technically possible, Microsoft used the 4.0 release to fix a number of outstanding bug fixes, the kind that had the potential to break existing code. Your code could accidentally have a dependency on such a bug. It is very rare that it does, I've only seen a handful of questions at SO about such a mishap.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Ok thanks Hans, that makes sense. There is a change between frameworks 3.5 and 4.0 which does cause me a problem, I have the same code which works for 3.5 but doesn't for 4.0, which is why I wanted the assembly to still use 3.5. It's to do with using a web service, I think the NTLM authentication changed, so now with 4.0 I get a HTTP 401 error back whereas with 3.5 it worked. Looking at the HTTP requests with 4.0 there are 3 NTLM auth flags missing which were present with 3.5. – Mark J Apr 03 '14 at 14:20
0

I have a lot of .Net 4 projects using .Net 2 dll's just fine (Where the .Net 2 libraries also support legacy systems). So yes but I am unsure of the full compatibility across different versions.

kenjara
  • 402
  • 10
  • 18