September 6, 2024

Windows .NET version hell

First some background and perspective on what follows. I began on my Linux machine using C# and "dotnet" to build a simple program. This yielded an executable "fish" that I could run on linux as "./fish", just like any other program.
Nice.

When I take fish.exe to my Windows machine, it will not run and gives unhappy errors. I discover that I only have .NET 6 and 7 on my windows machine, whereas I am using .NET 8 on my Fedora machine. But there is more to the problem that this. Even when I install .NET 8 on my windows machine I keep getting the error and it seems to still be running .NET 7. This is as yet an unsolved problem. For the play by play as this developed, follow along.

Build on Linux, copy to Windows

I have a simple "hello world" type C# program. I can build and run it just fine on linux (from the command line mind you). I build it using exactly the following command, but using linux-x64 rather than win-x64. Here is what happened when I first tryed doing the build on my linux machine using "win-x64" as the specified runtime.
cd Fish
dotnet build -r win-x64
dotnet publish -c release -r win-x64
Very much to my surprise this works! I expected to have to find and install a win-x64 runtime.
cd Fish/bin/release/net8.0/win-x86
ls -l
-rw-r--r-- 1 tom tom   4608 Sep  6 15:14 fish.dll
-rwxr-xr-x 1 tom tom 110592 Sep  6 15:14 fish.exe
Both of these are different in size than the linux versions. However when I try this on my Windows 10 system, I again get:
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files (x86)\do
tnet\'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'C:\Users\Tom\fish.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'C:\Users\Tom\fish.runtimeconfig.json' file and specify the appropriate framework.
I poke around on the Windows system and find:
Directory of C:\Program Files\dotnet\shared\Microsoft.NETCore.App
06/22/2023  06:52 PM              6.0.18
06/22/2023  06:55 PM              7.0.7
Both of these directories have the hostpolicy.dll file.

The clue is this line in my .csproj:

net8.0
My build is targetting .NET 8 and my windows machine has only 6 and 7. The fix would be to install/update .NET Core on my windows machine. I get this on my windows machine:
dotnet --version
7.0.304
It seems sort of funny that .NET is more up to date on my linux machine than on my windows machine. Indeed, .NET 8 was released on November 14, 2023. So how do we install it?
winget install Microsoft.DotNet.SDK.8
I type this on the command line, but it complains that I should give my two letter country code (US) someplace

Install .NET 8 on windows

I go to the console on my windows 10 machine, fire up a browsers, and search for "install .NET 8 on windows". The latest seems to be 8.0.401 -- I select that, and it downloads an installer. I select and run the installer. It is giving me .NET 8.0 SDK (8.0.401). I do this as a non-admin user "tom". This works, but I still cannot run my app.

So I go back to the original page and select the .NET 8.0.8 runtime. I get an installer, and run it. But again, I installed the wrong thing. I got the .NET.ASP thing, what I really want is the .NET 8.0.8 runtime. So we have a third time a bat and try that.
This is sort of encouraging. I get:

tom@GILA C:\Program Files\dotnet\shared\Microsoft.NETCore.App>dir
 Directory of C:\Program Files\dotnet\shared\Microsoft.NETCore.App
06/22/2023  06:52 PM              6.0.18
06/22/2023  06:55 PM              7.0.7
09/06/2024  03:58 PM              8.0.8
               5 Dir(s)  1,665,184,071,680 bytes free
We have the new runtime, and it does have hostpolicy.dll.

My EXE still won't run, giving the same error -- and even after a reboot, I see this:

dotnet --version
7.0.304
cd C:\Program Files\dotnet
dir
Directory of C:\Program Files\dotnet
07/16/2024  09:05 PM           146,608 dotnet.exe
Well, I just installed "dotnet" on 9-6-2024, so this version is old. I log out and log back in as an admin user. I uninstall then reinstall the .NET runtime.

I use the windows Start button, search for "add or remove programs", then find .NET things to eradicate. I get rid of:

.NET 8 ASP package
I was also ready to get rid of "NET 7.0.7 SDK", but then I spotted ".NET Version Manager" on the list. Let's give it a try first ... This is dated 2015, marked as "Beta", and the start menu cannot find it, so I don't think this has any merit.

I install the .NET desktop runtime, and now I see:

dotnet --list-runtimes
Microsoft.AspNetCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
tom@GILA C:\Users\Tom\Downloads>dotnet --version
7.0.304
I reboot the computer (this is often a necessary step with Windows. This had no benefit.
Feedback? Questions? Drop me a line!

Tom's Computer Info / [email protected]