惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
C
Cybersecurity and Infrastructure Security Agency CISA
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
T
Threat Research - Cisco Blogs
C
Cisco Blogs
Recent Announcements
Recent Announcements
S
Securelist
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
P
Privacy & Cybersecurity Law Blog
宝玉的分享
宝玉的分享
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 热门话题
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LINUX DO - 最新话题
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Spread Privacy
Spread Privacy
PCI Perspectives
PCI Perspectives
Project Zero
Project Zero
I
Intezer
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
MyScale Blog
MyScale Blog
V
Vulnerabilities – Threatpost
Recorded Future
Recorded Future
T
Tenable Blog
Jina AI
Jina AI
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志

博客园 - 宏宇

C#中一个简单的Task C#中多线程Task详解 C#数据去重的5种方式 Automa – AI浏览器扩展工具 超好用的网页端自动化扩展工具-Automa HTTP 请求“报头”——Referer 和 Cookie Thread.Abort的.Net Core替代方法 元数据、数据元、数据源、源数据 IIS设置使某IP访问时,跳转到指定页面 IIS自定义错误页面 关闭PerfWatson2.exe 体验改善计划 C# 线程(Thread) 配置 sql server 最大内存 sqlserver内存最佳配置 Windows安装时调出系统的cmd功能 Shift+F10 windows无法安装到这个磁盘,选中的磁盘采用gpt分区 如何删除硬盘efi系统分区 site命令 win10系统图片打开方式为照片查看器的方法 不安装运行时运行.NET程序
使用vs2022将.net8的应用程序发布为一个单独文件
宏宇 · 2024-10-17 · via 博客园 - 宏宇

在使用.NetCore3.1时,可以通过设置以下工程配置文本来将项目发布为一个单独的应用程序文件:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
	  <PublishSingleFile>true</PublishSingleFile>
	  <RuntimeIdentifier>win-x86</RuntimeIdentifier>
	  <!--PublishTrimmed>true</PublishTrimmed-->
  </PropertyGroup>
 
</Project>

1.如果直接将.NetCore3.1升级为.net8,发布时可能会弹出如:不再需要使用Microsoft.NET.Sdk.WindowsDesktop SDK。请考虑将根项目元素的Sdk届性更改为“Microsoft.NET.Sdk”的错误。

将工程配置项目修改为:

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

  <PropertyGroup>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <PublishSingleFile>true</PublishSingleFile>
	  <RuntimeIdentifier>win-x86</RuntimeIdentifier>
	  <!--PublishTrimmed>true</PublishTrimmed-->
  </PropertyGroup>
</Project>

2.发布为单个文件时的配置如下:
在这里插入图片描述
3.发布时,如弹出以下错误:无法复制文件“……\userProject\obj\Release\net8.0-windows\win-x86\singlefilehost.exe”,原因是找不到该文件。需要在userProject.csproj中添加如下元素:

<SelfContained>true</SelfContained>

4.此时生成的文件还不是最终的,同时生成的依赖还有vcruntime140_cor3.dll及wpfgfx_cor3.dll。若需要将这两个库依赖也包含进去,则还需要添加元素:

<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>

同时,生成的独立文件体积也成倍增加。

5.裁剪体积:

只添加:

<PublishTrimmed>true</PublishTrimmed>

发布生成时报:
“启用剪裁时,不支持或不推荐使用 WPF。请转到 https://aka.ms/dotnet-illink/wpf 以了解详细信息。”

解决:
添加:

<_SuppressWpfTrimError>true</_SuppressWpfTrimError>

再次调试时出运行时错误:

NotSupportedException: Built-in COM has been disabled via a feature switch. See https://aka.ms/dotnet-illink/com for more information.

解决:

添加:

<BuiltInComInteropSupport>true</BuiltInComInteropSupport>

调试可以正常了。发布后运行,却不能正常启动。

解决:

添加:

<TrimMode>partial</TrimMode>

6.最终如下:

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

  <PropertyGroup>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <PublishSingleFile>true</PublishSingleFile>
	  <RuntimeIdentifier>win-x86</RuntimeIdentifier>
	  <SelfContained>true</SelfContained>
      <PublishTrimmed>true</PublishTrimmed>
      <_SuppressWpfTrimError>true</_SuppressWpfTrimError>
      <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
      <TrimMode>partial</TrimMode>
  </PropertyGroup>
</Project>

7.后记

6最终的版本发布的文件仍然过大,故后来没有采用这种方式,又发布选项进行修改,部署模式选择依赖框架。工程配置如下:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
	 <!--SelfContained>true</SelfContained-->
	 <!--PublishTrimmed>true</PublishTrimmed>
	 <TrimMode>partial</TrimMode>
	 <_SuppressWpfTrimError>true</_SuppressWpfTrimError>
	 <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
	 <IncludeContentInSingleFile>true</IncludeContentInSingleFile>
	 <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract-->
  </PropertyGroup>
</Project>

最终发布的文件只有十几MB,也包含了所有的外部DLL文件及资源,且运行正常。