Po upgradu na.NET 8.0.11 sestavení některých mých projektů selhalo…
Nastavení sestavení projektu fungovalo dobře někde kolem .NET 8.0.0. Později, s upgradem běhového prostředí .NET na novější verze .NET 8.0 a .NET 9.0 a upgradem na Visual Studio, některé projekty přestaly fungovat. Vypadá to, že zavedli přelomové změny v nástrojích pro sestavení. Logika je stále zdravá a typy sestavení jsou stejné, jen se nástroje pro sestavení začaly chovat trochu jinak. Jsou zapotřebí nové konfigurace sestavení a skripty sestavení.
Myslím , že změnu chování mohu najít někde mezi (.NET Framework 8.0.0/.NET SDK 8.0.100) a (.NET Framework 8.0.11/.NET SDK 8.0.404). Ne všechna, ale některá sestavení projektu selhala.
Typické prostředí, na které se tento článek vztahuje, je C#/VS2022.
.NET verze 8.0.11 nebo novější
A vytváříte typ projektu SelfContained nebo SingleFile
Dostanete chyby/výjimky :
Knihovna 'hostpolicy.dll' potřebná ke spuštění aplikace nebyla nalezena
chyba MSB4018: …. Nelze najít soubor ….. 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'.
Vypadá to, že příznak <PublishSingleFile>true</PublishSingleFile> v souboru projektu .csproj v některých případech přestal fungovat. Poté jsem se spoléhal na tento příznak ve svých skriptech pro sestavení. Žádný problém, můžeme to předvídat, právě když víme, co můžeme očekávat.
Vypadá to, že proces sestavení vyvolal „dotnet publish“ z .NET SDK 9.* pro projekty, které jsem sestavoval pro framework .NET 8.*. Rozhodl jsem se tedy použít soubor global.json , abych explicitně určil, kterou sadu SDK chci použít.
Toto jsou stará nastavení sestavení, která fungovala v NET_8.0.0/SDK_8.0.100, ale přestala fungovat v novějších verzích
// 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. <!--ConsoleApp2C.csproj +++++++++++++++++++++++++++++++++++++--> <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0-windows</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <PlatformTarget>x64</PlatformTarget> <Platforms>AnyCPU;x64</Platforms> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <DebugType>embedded</DebugType> <PublishSingleFile>true</PublishSingleFile> <PublishTrimmed>true</PublishTrimmed> <IsTrimmable>true</IsTrimmable> <SelfContained>true</SelfContained> <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract> <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\ClassLibrary1\ClassLibraryA.csproj" /> </ItemGroup> <Target Name="PostBuild" AfterTargets="PostBuildEvent"> <Exec Command="echo +++Post-Build+++++
 if $(ConfigurationName) == Debug ( 
echo +++++Debug+++++ 
) 

 if $(ConfigurationName) == Release ( 
 echo +++++SelfContained_SingleFile_win-x64.cmd+++++ 
 call SelfContained_SingleFile_win-x64.cmd 
 echo +++++SelfContained_SingleFile_win-x64_Trimmed.cmd+++++ 
 call SelfContained_SingleFile_win-x64_Trimmed.cmd 
) " /> </Target> </Project> +++++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
Toto jsou nová nastavení sestavení, která fungují v NET_8.0.11/SDK_8.0.404
// NET_8.0.11/SDK_8.0.404 <!--ConsoleApp2C.csproj +++++++++++++++++++++++++++++++++++++--> <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0-windows</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <PlatformTarget>x64</PlatformTarget> <Platforms>AnyCPU;x64</Platforms> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <DebugType>embedded</DebugType> <PublishSingleFile>true</PublishSingleFile> <PublishTrimmed>true</PublishTrimmed> <IsTrimmable>true</IsTrimmable> <SelfContained>true</SelfContained> <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract> <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\ClassLibrary1\ClassLibraryA.csproj" /> </ItemGroup> <Target Name="RunAfterBuild1" AfterTargets="Build"> <Exec Command="call SelfContained_SingleFile_win-x64.cmd" Condition=" '$(BuildingInsideVisualStudio)' == 'true' "/> </Target> <Target Name="RunAfterBuild2" AfterTargets="Build"> <Exec Command="call SelfContained_SingleFile_win-x64_Trimmed.cmd" Condition=" '$(BuildingInsideVisualStudio)' == 'true' "/> </Target> </Project> +++++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" } }
Toto jsou nová nastavení sestavení, která fungují v NET_9.0.0/SDK_9.0.101
// NET_9.0.0/SDK_9.0.101 <!--ConsoleApp3C.csproj +++++++++++++++++++++++++++++++++++++--> <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net9.0-windows7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <PlatformTarget>x64</PlatformTarget> <Platforms>AnyCPU;x64</Platforms> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <DebugType>embedded</DebugType> <PublishSingleFile>true</PublishSingleFile> <PublishTrimmed>true</PublishTrimmed> <IsTrimmable>true</IsTrimmable> <SelfContained>true</SelfContained> <PublishReadyToRun>true</PublishReadyToRun> <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract> <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\ClassLibrary1\ClassLibraryA.csproj" /> </ItemGroup> <Target Name="RunAfterBuild1" AfterTargets="Build"> <Exec Command="call SelfContained_SingleFile_win-x64_ReadyToRun.cmd" Condition=" '$(BuildingInsideVisualStudio)' == 'true' " /> </Target> <Target Name="RunAfterBuild2" AfterTargets="Build"> <Exec Command="call SelfContained_SingleFile_win-x64_Trimmed_ReadyToRun.cmd" Condition=" '$(BuildingInsideVisualStudio)' == 'true' " /> </Target> </Project> +++++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" } }