Dot-Net-Core

.NET 5 未編譯為單個文件執行檔

  • September 30, 2020

我在通過 Visual Studio 調試時嘗試將我的 .NET 5 應用程序編譯為單個執行檔時遇到問題。

我的 .csproject 文件在下面。

<Project Sdk="Microsoft.NET.Sdk">

 <PropertyGroup>
   <OutputType>Exe</OutputType>
   <TargetFramework>net50</TargetFramework>
   <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
   <RuntimeIdentifier>win-x64</RuntimeIdentifier>
   <PublishSingleFile>true</PublishSingleFile>
   <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
   <PlatformTarget>x64</PlatformTarget>
 </PropertyGroup>

</Project>

我將執行時標識符設置為winx64並將單個文件設置為 true,但是在建構時,我留下了一堆我的應用程序使用的 DLL(總共 272 個)。我想知道 - 我如何將這些 DLL 打包到這個應用程序中?我曾認為將其發佈為單個文件執行檔已經可以做到這一點。

EXE旁邊的DLL圖片

對於 .NET 5,要在發布項目時獲取單個可執行的執行檔,重要的屬性是:

  • 發佈單個文件
  • 自給自足
  • IncludeAllContentForSelfExtract
  • 執行時標識符

您要麼需要將它們本身包含在項目文件中,要麼在命令行中指定它們。

項目文件:

<Project Sdk="Microsoft.NET.Sdk">

   <PropertyGroup>
       <OutputType>Exe</OutputType>
       <!--<OutputType>WinExe</OutputType>--><!--Use this for WPF or Windows Forms apps-->
       <TargetFramework>net5.0</TargetFramework>
       <!--<TargetFramework>net5.0-windows</TargetFramework>--><!--Use this for WPF or Windows Forms apps-->
       <PublishSingleFile>true</PublishSingleFile>
       <SelfContained>true</SelfContained>
       <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
       <RuntimeIdentifier>win-x64</RuntimeIdentifier><!--Specify the appropriate runtime here-->
   </PropertyGroup>

</Project>

命令行:

dotnet publish -r win-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeAllContentForSelfExtract=true

根據您的需求,還有其他值得考慮的屬性,例如:

  • 發布修剪
  • PublishReadyToRun

請參閱此處的文件頁面:

<https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file> <https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md>

引用自:https://stackoverflow.com/questions/64133771