.Net 5 发布单个文件 - 生成 exe 和 dll

Bai*_*ler 18 c# .net-core .net-5

我正在使用 VS 2019 和 .Net 5 来​​构建一个简单的控制台应用程序。我想与朋友分享这个应用程序,所以我尝试将它发布为单个文件,但我不断收到一些额外的 DLL,可执行文件需要正确运行这些 DLL。

编辑:将这个项目切换到 .net core 3.1 可以按预期工作我能够导出单个 Exe 文件而无需任何所需的 DLL。

点网 CLI: dotnet publish -c Release -o publish -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true

项目:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="HtmlAgilityPack" Version="1.11.28" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

输出

Ant*_*han 26

此处描述的已知问题:https : //github.com/dotnet/runtime/issues/36590

以及此处提供的新开发体验:https : //github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md#user-experience

因此,在您的情况下,您需要p:IncludeNativeLibrariesForSelfExtract=true额外使用。

完整命令:

dotnet publish -c Release -o publish -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true
Run Code Online (Sandbox Code Playgroud)

或在.csproj文件中包含此标志

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PlatformTarget>x64</PlatformTarget>
    
   <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>

</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)