Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable running on either .NET Framework 3.5 or 4.0

Tags:

c#

.net

We have various versions of our base application out in the field:

  • Version 1.0 comes bundled with .NET Framework 3.5
  • Version 2.0 comes bundled with .NET Framework 4.0

We distribute updates via an installer which embeds a .NET executable in them. The PC that this installer runs on may have the following combinations of our base application:

  • Both Version 1.0 and Version 2.0 (.NET 3.5 / .NET 4.0)
  • Version 1.0 only (.NET 3.5 only)
  • Version 2.0 only (.NET 4.0 only)

I need to ensure that the .NET executable embedded in the installer will run reliably.

The approaches I have found suggest adding the following to the app.config file:

<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
</startup>

A few questions:

  1. Will this mean that it will use .NET Framework 4.0 if it is present, otherwise resorting to .NET Framework 3.5 (or any previous frameworks that use the 2.0.50727 CLR)? IOW, are these tags ordered by priority?

  2. Does the version of the .NET Framework on the build machine have any run-time effect on the application, or does the hardcoding of the versions in the app.config file always take priority?

  3. What happens if I added code which relied upon a feature specific to .NET 4.0? Would including the 2.0.50727 version in the app.config (as shown above) result in a compile-time error, or a run-time error (only if .NET 4.0 wasn't installed)?

like image 749
LeopardSkinPillBoxHat Avatar asked Feb 14 '12 23:02

LeopardSkinPillBoxHat


1 Answers

  1. Yes, they're ordered by priority. It's easiest to try that with a simple console app:

    using System;
    
    class Test
    {
        static void Main()
        {
            Console.WriteLine(Environment.Version);
        }
    }
    

    Compile that with .NET 3.5, then run it with your current configuration, and it'll show 4.0 - then switch the lines and it'll show 2.0. See this MSDN article for more details.

  2. The version of the framework that you've targeted your app at is the important thing. If your config specifies that you support .NET 2, but you've targeted .NET 4, the application won't start.

  3. You won't have relied on a feature specific to .NET 4 without targeting .NET 4, in which case it won't run on .NET 2.

Point 3 isn't strictly accurate, in that there are things you can do which will differ based on which version of .NET is running. For example, change the app to:

using System;
using System.Collections.Generic;

class Test
{
    static void Main()
    {
        object o = new List<string>();
        Console.WriteLine(o is IEnumerable<object>);
    }
}

When running with .NET 2 that will print False (because IEnumerable<T> was invariant in .NET 2) but when running with .NET 4 it will print True because of generic covariance.

like image 117
Jon Skeet Avatar answered Oct 19 '22 07:10

Jon Skeet