.NET Core 3.0 and Single Binaries

.NET Core 3.0 allows for  single binary distribution on applications in cleaner and easier way than before

  • Check your .NET Version first dotnet --version

Results

3.0.100

  • Create a directory for your project mkdir native; cd native
  • Create a new project from the templates dotnet new console
  • Build and publish your application to a target dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true

Results

Microsoft (R) Build Engine version 16.3.0+0f4c62fea for .NET CoreCopyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 71.09 ms for C:\projects\native\native.csproj.native -> C:\projects\native\bin\Release\netcoreapp3.0\win-x64\native.dllnative -> C:\projects\native\bin\Release\netcoreapp3.0\win-x64\publish\
  • Compile for linux-x64 on a windows machinedotnet publish -r linux-x64 -c Release /p:PublishSingleFile=true

Results

Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 79.21 ms for C:\projects\native\native.csproj.native -> C:\projects\native\bin\Release\netcoreapp3.0\linux-x64\native.dllnative -> C:\projects\native\bin\Release\netcoreapp3.0\linux-x64\publish\

IMPORTANT to add to do tree shaking and optimization on the published binary

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>  
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>
</Project>

Now do the above commands with the PublishTrimmed option

C:\projects\native> dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true
Microsoft (R) Build Engine version 16.3.0+0f4c62fea for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 20.75 ms for C:\projects\native\native.csproj.
  native -> C:\projects\native\bin\Release\netcoreapp3.0\win-x64\native.dll
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
  native -> C:\projects\native\bin\Release\netcoreapp3.0\win-x64\publish\

Compile for linux-x64 on a windows machine

C:\projects\native> dotnet publish -r linux-x64 -c Release /p:PublishSingleFile=true
Microsoft (R) Build Engine version 16.3.0+0f4c62fea for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 70.29 ms for C:\projects\native\native.csproj.
  native -> C:\projects\native\bin\Release\netcoreapp3.0\linux-x64\native.dll
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
  native -> C:\projects\native\bin\Release\netcoreapp3.0\linux-x64\publish\

For a full list of the available Runtime Identifiers