Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow C# application built with .NET 2.0 to run on .NET 4.0/4.5

We have a C# DLL (let's call it myapp.exe) built with .NET 2.0 Framework (VS2005) and we found out that our application won't work on machines where only .NET 4.0 or above is installed. To let our application work on .NET 4.0, I added the following some lines to myapp.exe.config by following this article Installing .NET Framework V4.0 and Running .NET 2.0/3.0/3.5/3.5Sp1 Applications

<startup>
  <supportedRuntime version="v4.0.30319"/>    
</startup>

And it is working.

Then I saw that this article also mentioned the following (especially second paragraph):

Now, I knew that you can’t just take a 3.5 Service Pack 1 application and run it on the V4.0 CLR. It needs a V2.0 CLR or reconfiguring with a tag in order to bend the application to run on the V4.0 CLR and that bending might be something that you don’t want to do.

What I hadn’t realised though was that installing .NET 4.0 wouldn’t install the bits that you need for a 2.0/3.0/3.5/3.5Sp1 application. It would only install the V4.0 CLR and the V4.0 assemblies and not additionally install the equivalent of .NET Framework V3.5 Sp1. So, you’d need to install (e.g.) .NET Framework V3.5 Sp1 yourself along with .NET 4.0

From my testing it would mean that I could run my 2.0 C# application on .NET 4.0 with .NET 4.0 framework (4.0 assembly/libraries) which is contradicting to what the articles said.

Or am I missing something here? It could be helpful if someone could clarify on this. Microsoft doesn't really make this clear at all.

like image 681
windfly2006 Avatar asked Nov 19 '12 19:11

windfly2006


2 Answers

And I quote:

"The .NET Framework 4 is backward-compatible with applications that were built with the .NET Framework versions 1.1, 2.0, 3.0, and 3.5. In other words, applications and components built with previous versions of the .NET Framework will work on the .NET Framework 4."

Taken from Version Compatibility in the .NET Framework

You have the right idea with the App.config file, but your really limiting yourself with the one line.
Might I suggest a more liberal use of supportedRuntime lines?

For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v2.0.50727"/>
    <!-- 
    <supportedRuntime version="v3.5"/>  "The .NET Framework version 3.0 and 3.5 use version 2.0.50727 of the CLR."  
    -->
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.1,Profile=Client" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.1" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.2,Profile=Client" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.2" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.3,Profile=Client" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.3" /> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

Why is supportedRuntime version="v3.5" commented out? Remember, this configuration identifies which versions of the Common Language Runtime (CLR) your application is compatible with. There is no 3.0 or 3.5 version of the CLR. Refer to .NET Framework Versions and Dependencies

like image 104
Scott Solmer Avatar answered Sep 20 '22 06:09

Scott Solmer


From my testing it would mean that I could run my 2.0 C# application on .NET 4.0 with .NET 4.0 framework (4.0 assembly/libraries) which is contradicting to what the articles said.

You can run your application on .NET 4 using the .NET 4 assemblies. There is always the possibility that there may be a slight change in runtime behavior, however, as you won't be using the same runtime and framework which you used for development. I suspect the article is trying to suggest that you won't get the exact same behavior by just installing 4.0, though, as you've seen, it should work.

Doing this is likely fine, though I would recommend doing thorough testing of your application if this is going to be a standard deployment option.

like image 24
Reed Copsey Avatar answered Sep 23 '22 06:09

Reed Copsey