.net-core - .NET 5 未编译为单个文件可执行文件
问题描述
我在通过 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 打包到这个应用程序中?我曾认为将其发布为单个文件可执行文件已经可以做到这一点。
解决方案
对于 .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
推荐阅读
- javascript - Javascript/Jquery 在 flex 水平滚动 div 中添加鼠标和触摸可拖动/可滑动滚动
- jquery - 旋转外盒时旋转内盒
- shell - 这个 AppleScript/shell 代码是否以任何方式以纯文本形式公开输入?
- firebase - 如何调试 firestore.rules 变量和函数?
- swift - 如何从 AnyHashable 类型的字典中获取数据?
- linux - 将 CSV 文件中的第一行和第三行转换为 shell 脚本中的列
- java - 在详细信息表单上进行用户导航的最佳入门
- java - 来自 SD 卡的图像未加载到 WebView 中
- python - 使用循环计算数据点
- python - “if”条件语句上的数据类型错误