11

Lets imagine that we have two assemblies:

  1. Foo.Logic (compiled on .NET 2.0 framework)
  2. Foo.Application (compiled on .NET 4.0 framework) that have reference and uses compiled Foo.Logic.

Does it have impact on Foo.Application performance (or have any other drawbacks)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dariusz
  • 15,573
  • 9
  • 52
  • 68
  • 1
    Also see: [What happens when .NET 4.0 references a .NET 2.0 assembly?](http://stackoverflow.com/questions/2653566/what-happens-when-net-4-0-references-a-net-2-0-assembly) – Cody Gray - on strike Jun 02 '11 at 15:39

2 Answers2

20

In the situation you described in the question, everything will Just Work™ without any problems.

A .NET 4.0 application will load a .NET 2.0 library directly into the .NET 4.0 runtime environment. It will not use side-by-side execution unless you explicitly ask it to. There's a lot of misinformation or unclear statements made about this around the web, so be careful in evaluating what you read.

I finally came across this article, which confirms what Jon Skeet says in his answer. In particular, the article explains (emphasis added):

In-Proc SxS does not solve the compatibility problems faced by library developers. Any libraries directly loaded by an application--either via a direct reference or an Assembly.Load--will continue to load directly into the runtime and AppDomain of the application loading it. This means that if an application is recompiled to run against the .NET Framework 4 runtime and still has dependent assemblies built against .NET 2.0, those dependents will load on the .NET 4 runtime as well. Therefore, we still recommend testing your libraries against all version[s] of the framework you wish to support. This is one of the reasons we have continued to maintain our high level of backward compatibility.

But if you're interested in even more of the nitty-gritty details, it's worth mentioning that one of the new features introduced with version 4.0 of the CLR is the ability to run multiple versions of the runtime at the same time from in a single process. This is called "In Process Side-by-Side Execution" (or "Inproc SxS" if you're very cool), and is a very powerful tool. You can read more about it here on the CLR team's blog.

Here are some pretty pictures for demonstration purposes:

Note the "layer-cake" model that was used to build the .NET 3.0 and 3.5 releases on top of .NET 2.0. That means that they'll all run side-by-side without any problems. But the isolation of the other versions will create a problem.

However, as mentioned above, .NET 4.0 solves this problem with Side-by-Side Execution:

Of course, this is mostly applicable to situations involving COM and interop with external applications like Outlook, but it's still a very cool feature.

But either way, you shouldn't see any problems whatsoever with this approach, either with compatibility, performance, or whatever else.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Great, but what about running 1.1 version from 2.0 version assembly? – Dariusz Jun 02 '11 at 15:30
  • 1
    Yes it *is* supported - you can use an assembly compiled with .NET 1.1 from within a .NET 2.0 app with no issues. Likewise although CLR SxS *does* exist, it's not going to happen automatically. If a .NET 4 app references a .NET 2 assembly, it's normally going to load the .NET 2 assembly into the .NET 4 CLR. – Jon Skeet Jun 02 '11 at 15:44
  • @Jon: Are you sure that it's going to load the .NET 2 assembly into the .NET 4 CLR? My understanding is that only happens when the App.Config file is expressly configured to allow such. – Cody Gray - on strike Jun 02 '11 at 15:46
  • @Cody: Yes, absolutely sure. Try it! Build a class library targeting .NET 4, and then try to reference it from a .NET 4 application. It'll work seamlessly. – Jon Skeet Jun 02 '11 at 15:47
  • @Jon: Hrm, interesting. It appears there's lots of misleading information around the web about that. Probably because it "Just Works", no one has spent much time researching the details. – Cody Gray - on strike Jun 02 '11 at 15:52
  • 1
    @Cody Gray: Basically I think the SxS bit here is a red herring. Yes, as of .NET 4 you *can* run two CLRs side by side, but you generally *wouldn't*. It's for scenarios such as Outlook (as per the figure) where it's loading plugins. Each plugin is likely to end up in a different AppDomain to start with - but two plugins targeting the same version of .NET can share a CLR. – Jon Skeet Jun 02 '11 at 15:58
  • @Jon: Yeah, I think a lot of the misunderstanding comes from the way things work with COM. I found an article that seems to address some of the confusion, and I've updated my answer. I also removed my earlier comment that was inaccurate. Thanks for pointing that out! – Cody Gray - on strike Jun 02 '11 at 16:10
  • @Cody: You might want to move the bottom bit of the answer to the top, actually. That way anyone who's in the 99% of situations where they're just loading a library as normal will see the non-SxS bit first. – Jon Skeet Jun 02 '11 at 16:52
7

Unless you use explicitly use SxS, your .NET 2 assembly will be loaded into the .NET 4 CLR. There shouldn't be any performance problems at all... the code which originally targeted .NET 2 will just run using the .NET 4 libraries and CLR, etc. And yes, you can use an assembly compiled with .NET 1.1 in a .NET 2 or higher app, too.

There is a problem if you try to load a mixed-mode assembly (some native code, some IL) in .NET 4, IIRC... but if it's "just" a .NET class library, you should be fine.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194