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

问题描述 投票:0回答:1

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

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

Dotnet CLI:

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

Csproj:

<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>

Output

c# .net-core .net-5
1个回答
60
投票

此处描述的已知问题: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

或将此标志包含在

.csproj
文件中

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

</PropertyGroup>
© www.soinside.com 2019 - 2024. All rights reserved.