使用vs2022将.net8的应用程序发布为一个单独文件

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

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1TargetFramework>
    <UseWPF>trueUseWPF>
	  <PublishSingleFile>truePublishSingleFile>
	  <RuntimeIdentifier>win-x86RuntimeIdentifier>
	  
  PropertyGroup>
 
Project>

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

将工程配置项目修改为:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1TargetFramework>
    <UseWPF>trueUseWPF>
	  <PublishSingleFile>truePublishSingleFile>
	  <RuntimeIdentifier>win-x86RuntimeIdentifier>
	  
  PropertyGroup>
Project>

2.发布为单个文件时的配置如下:
使用vs2022将.net8的应用程序发布为一个单独文件_第1张图片
3.发布时,如弹出以下错误:无法复制文件“……\userProject\obj\Release\net8.0-windows\win-x86\singlefilehost.exe”,原因是找不到该文件。需要在userProject.csproj中添加如下元素:

<SelfContained>trueSelfContained>

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

<IncludeAllContentForSelfExtract>trueIncludeAllContentForSelfExtract>

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

5.裁剪体积:

只添加:

<PublishTrimmed>truePublishTrimmed>

发布生成时报:
“启用剪裁时,不支持或不推荐使用 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>trueBuiltInComInteropSupport>

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

解决:

添加:

<TrimMode>partialTrimMode>

6.最终如下:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1TargetFramework>
    <UseWPF>trueUseWPF>
	  <PublishSingleFile>truePublishSingleFile>
	  <RuntimeIdentifier>win-x86RuntimeIdentifier>
	  <SelfContained>trueSelfContained>
      <PublishTrimmed>truePublishTrimmed>
      <_SuppressWpfTrimError>true_SuppressWpfTrimError>
      <BuiltInComInteropSupport>trueBuiltInComInteropSupport>
      <TrimMode>partialTrimMode>
  PropertyGroup>
Project>

你可能感兴趣的:(.Net技术,.net,.netcore)