.NET Versioning Riddle

September 1, 2017 on 10:10 am | In Uncategorized | No Comments

We have a .NET application compiled with .NET Framework 4.5 as a target.

The riddle is: why does this application work on Windows 7 with only .NET Framework 4.0 installed but not on Windows Server 2003 with the same .NET Framework 4.0 installed?

The answer it twofold. The first part is easy:

1. Why does the application compiled with .NET Framework 4.5 as a target even works with .NET Framework 4.0?

It is because the CLR is still the same. It is 4.0. Have a look with ildasm at a simple exe or dll targeted for .NET Fraemwork 4.5 and you will see:

image

So the app will work but, as Microsoft puts it in https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/versions-and-dependencies “We do not recommend running apps that target a later version of the .NET Framework on the an earlier version of the .NET Framework.” So it will work up until the time where you access some specific .NET 4.5 functionality where it will crash.

So I guess if you want your app to be specific about the minimal .NET Framework Version it needs, you will have to check it yourself. For example as suggested in here: https://www.hanselman.com/blog/NETVersioningAndMultiTargetingNET45IsAnInplaceUpgradeToNET40.aspx

2. The second part of the question is more tricky. Why does it work with .NET Framework 4.0 in Windows 7 but not with the same .NET Framework 4.0 in Windows Server 2003?

As it turnes out (I have not found the answer at Microsoft but on StackOverflow: https://stackoverflow.com/questions/17499351/is-it-possible-to-run-a-net-4-5-app-on-xp) the compiler for the app targeting .NET Framework 4.5 sets the file header (https://msdn.microsoft.com/en-us/library/ms809762.aspx) to be compatible with Windows Version 6.0 (Vista and up – https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions) or newer. You can check it out by using dumpbin /headers from VS command line:

image

4.00 operating system version
0.00 image version
4.00 subsystem version

If you compile the app with .NET Framework 4.0 as a target the file header looks like this:

image

4.00 operating system version
0.00 image version
6.00 subsystem version

So the minimal Windows version is 4.0 being Windows 95 and up.

Powered by WordPress with Pool theme design by Borja Fernandez.
Text © Marcin Kawalerowicz. Hosting CODEFUSION.