.NET Error: Assembly is built by a runtime newer than the currently loaded runtime

I have upgraded a C# console app to .NET 4.5.1.  In the process, I removed all external library references and re-added new references to .NET 4.x versions of the DLLs.

The new 4.5.1 console app works properly on my development server, and I have tested it on 5 other machines, where it works fine as well.

However, when I deploy it to my client's workstation, it crashes immediately with this error:

Unhandled Exception:  System.BadImageFormatException: Could not load file or assembly 'myapp.exe' or one of its dependencies.  This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

I have checked that the client workstation has .NET 4.5.x installed (it shows version 4.5.2 in the registry (release 379893), and I've triple checked my DLL references.  My guess is that some DLL is different on the client machine.

As a test, I tried it on a second client workstation, and the same error occurred.  The second workstation was built by the same IT guy using the same files / installs, so we didn't get any new clues, but it tells me that the issue is not a random workstation-specific issue.

Rather than troubleshoot indirectly by guessing and trial and error, is there a tool to identify which DLL is causing this error?

Will Sysinternals Process Monitor or some other tool be able to identify the culprit?
LVL 18
Steve EndowMicrosoft MVP - Dynamics GPAsked:
Who is Participating?
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
After pondering the .NET 2 activity and doing some searching on the issue, I found the cause of the problem.

The .NET version is set in the exe.config file--something I completely forgot about.

When I deployed the new version of the console app, I only copied the new EXE and DLL files--I did not replace or update the exe.config file, so it still had the .NET 2.0 reference in it:

<startup><supportedRuntime version="v2.0.50727"/></startup><applicationSettings>

I modified the config file to read:

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

After that change, the app worked fine.

Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
I used Process Monitor on my development machine and on the client workstation to capture a trace of the EXE activity for comparison.

The only significant difference I'm seeing is that on the workstation, it appears to be loading / referencing .NET 2.0, whereas on my dev machine, it's clearly hitting .NET 4.

In the attached image, the activity on the left is my dev machine, on the client workstation is on the right.  On the left, there are numerous references to .NET 4, but on the right, it's mostly v2.0 with only a few brief references to .NET 4.

It seems that for some reason, the client workstation is thinking the app is .NET 2 instead of .NET 4.x.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.