This shows how to use the various publish options when building a netcore 3 console app.
- This sample references and makes use of NodaTime to illustrate a dependency being consumed.
- This sample references, but does not use, Newtonsoft to illustrate a dependency being trimmed.
- The Runtime IDentifier is hard coded to
win10-x64
. All profiles will inherit this setting. PublishDir
is set tosrc\MyConsole\publish\$(PublishProfile)\
.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<PublishDir>publish\$(PublishProfile)\</PublishDir>
</PropertyGroup>
<ItemGroup>
<Content Include="ContentFile.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NodaTime" Version="2.4.7" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
</ItemGroup>
</Project>
Publish options are stored in Publish Profiles and are located in src/MyConsole/Properties/PublishProfiles
Uses an empty (default) publish profile:
<Project>
<PropertyGroup>
</PropertyGroup>
</Project>
- Files: 234
- Size: 71.3 MB
Publish Command:
dotnet publish MyConsole\MyConsole.csproj -c Release /p:PublishProfile=Default
Same as the Default but makes it Framework-dependent.
For an FDD, you deploy only your app and third-party dependencies. Your app will use the version of .NET Core that's present on the target system.
<Project>
<PropertyGroup>
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>
- Files: 14
- Size: 2.36 MB
Notes:
- Depends on an installed runtime.
Publish Command:
dotnet publish MyConsole\MyConsole.csproj -c Release /p:PublishProfile=Fdd
Same as Default but creates a Single-file executables.
The executable is self-extracting and contains all dependencies (including native) that are required to run your app. When the app is first run, the application is extracted to a directory based on the app name and build identifier. Startup is faster when the application is run again. The application doesn't need to extract itself a second time unless a new version was used.
<Project>
<PropertyGroup>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
</Project>
- Files: 1
- Size: 71.32 MB
Notes:
Publish Command:
dotnet publish MyConsole\MyConsole.csproj -c Release /p:PublishProfile=SingleFile
Combines Single-File and Framework Dependent.
<Project>
<PropertyGroup>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>
- Files: 1
- Size: 2.36 MB
Notes:
- Depends on an installed runtime.
Publish Command:
dotnet publish MyConsole\MyConsole.csproj -c Release /p:PublishProfile=SingleFileFdd
Same as the Default but uses assembly-linking.
The .NET core 3.0 SDK comes with a tool that can reduce the size of apps by analyzing IL and trimming unused assemblies. Self-contained apps include everything needed to run your code, without requiring .NET to be installed on the host computer. However, many times the app only requires a small subset of the framework to function, and other unused libraries could be removed.
<Project>
<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
</Project>
- Files: 124
- Size: 38.44 MB
Publish Command:
dotnet publish MyConsole\MyConsole.csproj -c Release /p:PublishProfile=Trimmed
Combines Single File and Trimmed:
<Project>
<PropertyGroup>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
</Project>
- Files: 1
- Size: 38.45 MB
Publish Command:
dotnet publish MyConsole\MyConsole.csproj -c Release /p:PublishProfile=SingleFileTrimmed
ReadyToRun images (<PublishReadyToRun>true</PublishReadyToRun>
) are not covered in the above scenarios, but should be considered as an option for production apps.
R2R binaries improve startup performance by reducing the amount of work the JIT needs to do as your application is loading. The binaries contain similar native code as what the JIT would produce, giving the JIT a bit of a vacation when performance matters most (at startup). R2R binaries are larger because they contain both intermediate language (IL) code, which is still needed for some scenarios, and the native version of the same code, to improve startup. - https://devblogs.microsoft.com/dotnet/announcing-net-core-3-0/