Your resource for web content, online publishing
and the distribution of digital products.
S M T W T F S
 
 
 
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
31
 
 

How to Troubleshoot 'hostpolicy.dll' and 'singlefilehost.exe' Errors in .NET 8.0.11

DATE POSTED:January 3, 2025
Breaking Changes in .NET Build Tools From 8.0.0 to 8.0.11

After upgrading to .NET 8.0.11, the build for some of my projects failed…

1 Problem description

Build project settings all worked well somewhere around .NET 8.0.0. Later, with the upgrade of .NET runtime to later versions of .NET 8.0 and .NET 9.0 and an upgrade to Visual Studio, some of the projects stopped working. It looks like they introduced breaking changes in the build tools. Logic is still sound and build types are the same, just the build tools started to behave a bit differently. New build configurations and build scripts are needed.

\ I think I can locate the change in behavior somewhere between (.NET Framework 8.0.0/.NET SDK 8.0.100) and (.NET Framework 8.0.11/.NET SDK 8.0.404). Not all, but some project builds failed.

1.1 The environment

The typical environment to which this article applies is C#/VS2022.

.NET version 8.0.11 or later

And you are building project type SelfContained or SingleFile

1.2 Problem manifestation

You get Errors/Exceptions:

  • The library 'hostpolicy.dll' required to execute the application was not found

  • error MSB4018: …. Could not find file ….. singlefilehost.exe

    \

+++Error1,When running the application:++++++++++++++++++++ A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet\'. Failed to run as a self-contained app. - The application was run as a self-contained app because 'C:\tmpNetBundle\BundleExample02_NET90\ConsoleApp2C\ SelfContained_SingleFile_win-x64\ConsoleApp2C.runtimeconfig.json' was not found. - If this should be a framework-dependent app, add the 'C:\tmpNetBundle\BundleExample02_NET90\ConsoleApp2C\ SelfContained_SingleFile_win-x64\ConsoleApp2C.runtimeconfig.json' file and specify the appropriate framework. PS C:\tmpNetBundle\BundleExample02_NET90\ConsoleApp2C\ SelfContained_SingleFile_win-x64> +++Error2,During build++++++++++++++++++++ error MSB4018: The "GenerateBundle" task failed unexpectedly. [C:\tmpNetBundle\BundleExample01_NET_8.0.0_SDK_8.0.100\ConsoleApp2\ConsoleApp2.csproj] error MSB4018: System.IO.FileNotFoundException: Could not find file 'C:\tmpNetBundle\BundleExample01_NET_8.0.0_SDK_8.0.100\ConsoleApp2\ obj\Release\net8.0-windows\win-x64\singlefilehost.exe'.

\

1.3 Cause of problem and resolution

It looks like the flag true in the project file .csproj stopped to work in some cases. I was relying on that flag in my build scripts after that. No problem, we can anticipate that thing, just when we know what to expect.

\ It looks like the build process invoked “dotnet publish” from .NET SDK 9.* for projects that I was building for .NET 8.* framework. So, I decided to use global.json file to explicitly specify which SDK I want to use.

2 Code Samples

These are old build settings that worked in NET8.0.0/SDK8.0.100 but stopped working in later versions

// NET_8.0.0/SDK_8.0.100 // NOTE: These project settings all worked well somewhere around .NET 8.0.0. Later, with the upgrade of .NET runtime to later versions of .NET 8.0 and .NET 9.0 and an upgrade to Visual Studio, some of projects stopped working. It looks like they introduced breaking changes in the build tools. Logic is still sound and build types are the same, just the build tools started to behave a bit differently. A new build configurations and build scripts are needed. Exe net8.0-windows enable enable x64 AnyCPU;x64 win-x64 embedded true true true true true true +++++Script: SelfContained_SingleFile_win-x64.cmd dotnet publish ConsoleApp2C.csproj --no-build --runtime win-x64 --configuration Release -p:PublishSingleFile=true -p:SelfContained=true -p:PublishReadyToRun=false -p:PublishTrimmed=false --output ./SelfContained_SingleFile_win-x64 +++++Script: SelfContained_SingleFile_win-x64_Trimmed.cmd dotnet publish ConsoleApp2C.csproj --no-build --runtime win-x64 --configuration Release -p:PublishSingleFile=true -p:SelfContained=true -p:PublishReadyToRun=false -p:PublishTrimmed=true --output ./SelfContained_SingleFile_win-x64_Trimmed

\ These are new build settings that work in NET8.0.11/SDK8.0.404

// NET_8.0.11/SDK_8.0.404 Exe net8.0-windows enable enable x64 AnyCPU;x64 win-x64 embedded true true true true true true +++++Script: SelfContained_SingleFile_win-x64.cmd echo .NET SDK version: dotnet --version dotnet publish ConsoleApp2C.csproj --nologo --no-restore --runtime win-x64 --configuration Release -p:PublishSingleFile=true -p:SelfContained=true -p:PublishReadyToRun=false -p:PublishTrimmed=false --output ./SelfContained_SingleFile_win-x64 +++++Script: SelfContained_SingleFile_win-x64_Trimmed.cmd echo .NET SDK version: dotnet --version dotnet publish ConsoleApp2C.csproj --nologo --no-restore --runtime win-x64 --configuration Release -p:PublishSingleFile=true -p:SelfContained=true -p:PublishReadyToRun=false -p:PublishTrimmed=true --output ./SelfContained_SingleFile_win-x64_Trimmed +++++Configfile: global.json { "sdk": { "version": "8.0.404" } }

\ These are new build settings that work in NET9.0.0/SDK9.0.101

// NET_9.0.0/SDK_9.0.101 Exe net9.0-windows7.0 enable enable x64 AnyCPU;x64 win-x64 embedded true true true true true true true +++++Script: SelfContained_SingleFile_win-x64_ReadyToRun.cmd echo .NET SDK version: dotnet --version dotnet publish ConsoleApp3C.csproj --nologo --no-restore --runtime win-x64 --configuration Release -p:PublishSingleFile=true -p:SelfContained=true -p:PublishTrimmed=false -p:PublishReadyToRun=true --output ./SelfContained_SingleFile_win-x64_ReadyToRun +++++Script: SelfContained_SingleFile_win-x64_Trimmed_ReadyToRun.cmd echo .NET SDK version: dotnet --version dotnet publish ConsoleApp3C.csproj --nologo --no-restore --runtime win-x64 --configuration Release -p:PublishSingleFile=true -p:SelfContained=true -p:PublishReadyToRun=true -p:PublishTrimmed=true --output ./SelfContained_SingleFile_win-x64_Trimmed_ReadyToRun +++++Configfile: global.json { "sdk": { "version": "9.0.101" } }

\