tags:

views:

86

answers:

2

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 is 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)

A: 

You mention that your setup program contains a launch condition that verifies .NET >= 3.5 is present on the machine. So your setup knows what the highest CLR on the machine is. Lets call that variable NETFXVER

You can write a custom action that uses NETFXVER to modify the installed app.config, so it targets the highest CLR on the machine.

Alternatively, you can package several app.config files in your setup, each targeting a supported version of the CLR, and bind each one to the appropriate condition using NETFXVER so only the proper one is deployed during installation.

Frédéric Hamidi
Currently, the installation only checks the existence of 3.5 or higher directory under %SYSTEMROOT%\Microsoft.NET\Framework. While your solution is certainly an option, I won't go this way, as I have another solution using one app,.config file, and I don't want to complicate the installation.
splintor
+2  A: 

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.

splintor
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.
ChrisF