Publish .NET Core App As Portable Executable

前端 未结 3 1369
暖寄归人
暖寄归人 2020-12-01 07:05

I have a simple .net core app and publish it by following command:

 dotnet publish -c Release -r win10-x64

SqlLocalDbStarter.csproj

3条回答
  •  一整个雨季
    2020-12-01 07:20

    .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 is PublishTrimmed=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 your csproj file:

    
      win-x64
      true
    
    

    or on the command line in a folder with csproj file:

    dotnet publish -r win-x64 -p:PublishSingleFile=true
    

    For more details see a great answer given by Gopi.

    Standalone utils

    Warp (thanks to Darien Shannon for mentioning it in the comment) and dotnet CoreRT. Both work with previous versions of .Net Core also

    Warp

    It is a tool similar to ILMerge for the classic .NET Framework. It is very easy to use. For the 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 pre-compile the application into a native single-file executable using dotnet CoreRT project. I'm saying "try" because documentation says:

    This project is in the early stages of its development.

    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:

    dotnet new nuget 
    

    This will add a nuget.config file to your application. Open the file and in the element under add the following:

    
    
    

    Then run this:

    dotnet add package Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-* 
    

    Then run this:

    dotnet publish -r win-x64 -c release
    

    Once completed, you can find the native executable in the root folder of your project under /bin/x64//netcoreapp2.0/publish/

提交回复
热议问题