How to publish a .NET project for Windows and Linux, using a single restore and build operation.
I've just published a detailed guide on how to efficiently publish a cross-platform .NET 8 application to both Windows and Linux using a single NuGet restore and a single MSBuild operation.
?? Dive into the full details on GitHub for a comprehensive step-by-step walkthrough: GitHub Link.
One of the most complex and fascinating challenges was resolving the issue with the apphost for Linux.
?? To wrap it up, here are the final commands you'll need to publish your .NET application to both Windows and Linux using a single NuGet restore and MSBuild operation.
RIDs in .csproj file:
<RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>
Build commands:
dotnet restore .\GenerateSelfSignedCertificate\GenerateSelfSignedCertificate.csproj -v n
msbuild .\GenerateSelfSignedCertificate\GenerateSelfSignedCertificate.csproj /p:Configuration=Debug /p:RestorePackages=false
msbuild .\GenerateSelfSignedCertificate\GenerateSelfSignedCertificate.csproj /t:publish /p:RestorePackages=false /p:NoBuild=true /p:PublishProfile=.\GenerateSelfSignedCertificate\Properties\PublishProfiles\FolderProfile_windows.pubxml /p:OutDir=.\x64\Debug\net8.0 /p:AppendRuntimeIdentifierToOutputPath=false -v:n
msbuild .\GenerateSelfSignedCertificate\GenerateSelfSignedCertificate.csproj /p:Configuration=Debug /t:ResolveFrameworkReferences;_CreateAppHost /p:PublishProfile=.\GenerateSelfSignedCertificate\Properties\PublishProfiles\FolderProfile_linux.pubxml /p:OutDir=.\x64\Debug\net8.0 /p:AppendRuntimeIdentifierToOutputPath=false -v:n
msbuild .\GenerateSelfSignedCertificate\GenerateSelfSignedCertificate.csproj /t:publish /p:RestorePackages=false /p:NoBuild=true /p:PublishProfile=.\GenerateSelfSignedCertificate\Properties\PublishProfiles\FolderProfile_linux.pubxml /p:OutDir=.\x64\Debug\net8.0 /p:AppendRuntimeIdentifierToOutputPath=false -v:n
Thanks to Martin B. Radim Bernatik Konstantin Yasyuk for the help.
#NET #MsBuild #dotnet #nuget
Software Engineer at Veeam Software
2 个月Hello. I reviewed your article, and the "final commands" section seems overly complicated. I worked on improving your solution to achieve the same result with the following commands: dotnet restore dotnet build --no-restore dotnet msbuild -t:PublishAllRids Here is commit with my changes: https://github.com/FrediKats/net_publish/commit/25a4b912068ec53d364e50c4e7197bea7779ce25 List of changes: 1. Used ArtifactsOutput to manage paths. This makes it easier to clean up all artifacts generated by dotnet CLI commands. 2. Replaced command for calling "_CreateAppHost" with MSBuild flag "_UpdateAppHostForPublish=true" (build-in MSBuild flag that force _CreateAppHost during publish) 3. Added a custom target to execute publishing to all runtime identifiers in parallel (more about it: https://github.com/dotnet/sdk/issues/9363).