How to publish a .NET project for Windows and Linux, using a single restore and build operation.

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

Alexey Ivnitskiy

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).

要查看或添加评论,请登录

Mike Bryksin的更多文章

  • Essential resources for aspiring c# junior developers

    Essential resources for aspiring c# junior developers

    Hey future devs! ?? If you're gearing up to land your first role as a junior software developer, check out this list of…

    2 条评论
  • Just one more interview coding task

    Just one more interview coding task

    As previously announced, I'd like to share an elegant solution for a coding interview task that has caught my…

  • powershell.exe and tls 1.3

    powershell.exe and tls 1.3

    Wondering how to make your powershell.exe cmdlets compatible with tls 1.

社区洞察

其他会员也浏览了