I have a simple .net core app and publish it by following command:
dotnet publish -c Release -r win10-x64
SqlLocalDbStarter.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
</ItemGroup>
</Project>
When publish process finished dotnet create win10-x64
folder at bin\Release
folder then after open it the folder contains publish
folder and some dll and exe files.
There are some issue for me:
- Which one of
exe
files (inside / outside publish folder) i need to PE app? - Why when i cut
exe
file and move it in other place it doesn't run (without message)? - If I need all of
dll
files to run application, so there are tow options for me (inside / outside publish folder), inside publish folder size is 66 MB but outside publish folder is 1 MB. - I want to have a one
exe
file to run my program without dll files.
Net.Core 3.0
Net.Core 3.0 supports it out of the box. It packs all stuff in one
.exe
file (~68 MB for a basic console app). There isPublishTrimmed=true
option that can decrease size to ~28 MB by analyzing static code references and excluding unused framework assemblies from the final build.To configure
single exe
build edit yourcsproj
file:or on the command line in a folder with
csproj
file:For more details see a great answer given by Gopi.
Standalone utils
Warp
(thanks to Darien Shannon for mentioning it in the comment) anddotnet CoreRT
. Both work with previous versions of .Net Core alsoWarp
It is a tool similar to ILMerge for the classic
.NET Framework
. It is very easy to use. For basic console app It can produce.exe
~35 MB without tree shaker and around 10-15 MB with tree shaker.Dotnet CoreRT
For now you can try to precompolie the application into a native single-file executable using
dotnet CoreRT
project. I'm saying "try" because documentation says:Nevertheless, it works at least for simple applications. See the sample here. According to its description you need to run the following command in the project folder:
Then run this:
Then run this:
This documentation from Microsoft uses the same
dotnet publish -c Release -r win10-x64
that you have used, and documents it as follows (emphasis added):So the correct files to deploy are the ones in the
publish
subdirectory. That directory is 60+ MB because it includes the .NET core libraries needed for self-contained deployment.Before .NET Core 3.0
Pretty self explanatory:
So this works right, we end up with a folder that has our exe and everything that is required to run it, but the issue is that there is a tonne required to run even a HelloWorld console app.
After .NET Core 3.0
All this does is runs our publish command but tells it to package it within a single file. You’ll notice that we no longer specify the self-contained flag. That’s because it’s assumed that if you are packaging as a single exe, that you will want all it’s dependencies along with it. Makes sense.
A single tidy exe! When this is executed, the dependencies are extracted to a temporary directory and then everything is ran from there. It’s essentially a zip of our previous publish folder! I’ve had a few plays around with it and honestly, it just works. There is nothing more to say about it. It just works.
File Size And Startup Cost
Modify the csproj and add PublishTrimmed = true.
Now run the below command:
Reference: