.NET 5 not compiling to single file executables

Fer*_*xxo 14 roslyn .net-core .net-core-publishsinglefile asp.net-core-5.0

Haiya! I'm having an issue regarding trying to compile my .NET 5 application to a single file executable while debugging through Visual Studio.

My CSProject is bellow.

<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>
Run Code Online (Sandbox Code Playgroud)

I have my runtime identifier set to winx64 and publish single file set to true, yet when building I'm left with a bunch of DLLs that my application uses building along-side it (a total of 272 in total). I was wondering - how would I package these DLLs into this application? I had thought that publishing it as a single file executable would do that already-

EXE 旁边的 DLL 图片

Dan*_*Dan 25

对于 .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>
Run Code Online (Sandbox Code Playgroud)

命令行界面:

dotnet publish -r win-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeAllContentForSelfExtract=true
Run Code Online (Sandbox Code Playgroud)

根据您的需求,还有其他值得考虑的属性,例如:

  • 发布修剪
  • 发布准备运行

请参阅此处的文档页面:

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

  • 我的项目文件中缺少的行是 &lt;IncludeAllContentForSelfExtract&gt;。通过 VS UI 设置发布没有 IncludeAllContentForSelfExtract 参数,在 FolderProfile.pubxml 文件中手动添加该行是神奇的一步 (5认同)