How to force an application to use .NET 3.5 or above?
Asked Answered
C

4

15

Our application is built with VS 2008, uses Linq and has Target Framework set to .NET Framework3.5.

It works OK when only .NET 3.5 or 4 is installed on the machine.

However, on machines where both .NET 2 (or 3.0) and .NET 4 are installed, the application is loaded with .NET 2, and crashes when Linq is accessed, as it looks for the .NET 3.5 libraries.

Using the tag in app.config doesn't seem to help, as it specifies the CLR version, which is 2 in case of .NET 3.5.

Note that our installation verifies that .NET 3.5 or upper is installed.

Is there a way to tell the application to load:

  • the highest CLR it finds, or
  • CLR 4 if it is installed, and CLR 2 if CLR 4 is not installed, or
  • CLR 2 if .NET 3.5 is installed and CLR 4 if .NET 3.5 is not installed

(Note that similar question is left unanswered in the Community Content section of the Element documentation)

Caste answered 17/10, 2010 at 11:47 Comment(0)
C
16

Forming the question led me to the answer. As mentioned in the Element documentation,

When multiple versions of the runtime are supported, the first element should specify the most preferred version of the runtime, and the last element should specify the least preferred version.

So the way to achieve the second option ("CLR 4 if it is installed, and CLR 2 is CLR 4 is not installed") is to reverse the order of the elements in app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
        <supportedRuntime version="v2.0.50727"/>
    </startup>
</configuration>

This way, .NET 4 will be loaded if it is installed, and an earlier version will be loaded if not.

Caste answered 17/10, 2010 at 12:13 Comment(4)
You can't debug with this setup - it gives an error and .NET 4 isn't supported by VS2008. It works OK for running the release build outside VS though.Gilles
I've had experiences with apps failing to start if the <startup> element isn't at the end of the file (just before </configuration>), on Framework v3.5 at least. Worth trying moving this piece of config towards the end of the file if you hit startup problems.Prud
If I left only v4.0 as SupportedRuntime and in system is only v4.5 it will teke v.4.5 anyway? How to force of using just one version?Giacomo
@Giacomo According to msdn.microsoft.com/en-us/library/jj152935.aspx, 4.5 also uses "v4.0". This is because 4.5 (adn 4.5.1) is not really a new version, but a patch. You can't have 4 and 4.5 installed side-by-side - installing 4.5 patches the v4 installation. What are you trying to achieve?Caste
B
0

Another useful link is this page on MSDN. This shows all of the values required in app.config if you want to just target the client profile or if you require the full profile.

Boxthorn answered 11/6, 2012 at 14:39 Comment(0)
U
0

The .NET Framework version 3.0 and 3.5 use version 2.0.50727 of the CLR.

Unvalued answered 25/7, 2012 at 14:1 Comment(0)
C
-1

If you have an unmanaged EXE calling a .NET DLL, you need to create a foo.exe.config file as well, containing the above <startup>... block.

Comfortable answered 2/9, 2011 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.