views:

50

answers:

1

Ever since we started upgrading some of our projects to .NET framework 4.0, I've been running into a lot of issues regarding the following error:

This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

It's now happening with our installer application. The installer goes through a list of DLL's and installs the necessary ones on a target machine. Each assembly is loaded into context using

Assembly.LoadFile

The first assemblies in the queue are 3.5. Once the installer picks up a 4.0 DLL and tries to load it using Assembly.LoadFile, I get that error. It seems as though loading the first DLL sets the precedent as to which "runtime" is currently loaded.

Is there a way I can unload this currently loaded runtime before processing a 4.0 DLL in order to avoid this error?

+1  A: 

This will depend on what CLR is the executable that is loading the assemblies running. If it runs on CLR 2.0 you won't be able to load a .NET 4.0 assembly. So make sure that if you try to load a .NET 4.0 assembly you are running the application on CLR 4.0. You mention the installer application. There's a possibility to set a launch condition requirement for .NET 4.0:

alt text


UPDATE:

You could also force your executable to be loaded by the CLR 4.0 by putting this in the app.config file:

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

Because the CLR 4.0 supports loading assemblies compiled against previous versions this will allow you to work around the problem without recompiling the executable against .NET 4.0.

Darin Dimitrov
Right. The installer I mention is our own custom executable application; not a setup solution installer project. Can you setup launch conditions on any executable?
towps
The current runtime that the error refers to, this is the runtime used by the specific application is that correct?
towps
You need to compile this executable against .NET 4.0. By doing this it will automatically run under the CLR 4.0 (if available) and you will be able to load assemblies compiled against .NET 4.0.
Darin Dimitrov