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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
T
Troy Hunt's Blog
I
InfoQ
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
C
Cisco Blogs
G
GRAHAM CLULEY
The Register - Security
The Register - Security
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
小众软件
小众软件
雷峰网
雷峰网
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
博客园 - 聂微东
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
腾讯CDC
P
Palo Alto Networks Blog
Scott Helme
Scott Helme

博客园 - 宏宇

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文件及资源,且运行正常。