gpt4 book ai didi

.net-core - .NET 5 未编译为单个文件可执行文件

转载 作者:行者123 更新时间:2023-12-03 16:42:53 29 4
gpt4 key购买 nike

海呀!在通过 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,我的应用程序使用这些 DLL(总共 272 个)。我想知道 - 我如何将这些 DLL 打包到这个应用程序中?我原以为将它发布为单个文件可执行文件就可以做到这一点 -
Picture of DLLs along side EXE

最佳答案

对于 .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
    根据您的需求,还有其他值得考虑的属性,例如:
  • PublishTrimmed
  • 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

    关于.net-core - .NET 5 未编译为单个文件可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64133771/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com