首页 > 解决方案 > PublishSingleFile 不生成单个可执行文件

问题描述

我有一个 .Net Core 3 控制台应用程序,我正在尝试将其发布为自包含的单个可执行文件。我过去能够做到这一点,但令我惊讶的是它不再起作用。项目结构是一个带有两个程序集的控制台应用程序,都在 Core 3 中。

如果我使用dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true我希望输出是几个 mb 大小的单个可执行文件。但是,发布文件夹包含可执行文件(几百 kb)和一个 .dll 文件以及 .cache 文件和 pdb。

我的控制台应用程序的配置如下:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <LangVersion>latest</LangVersion>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <Nullable>enable</Nullable>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>

如果我直接从 Visual Studio 发布应用程序,我会得到与上面相同的结果。

所以我的问题归结为:为什么这个配置或发布语句不会产生一个自包含的单个可执行文件?

标签: c#visual-studio.net-core

解决方案


I can get my 3.1 console app to publish with the following:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>..\..\Binaries\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>True</PublishSingleFile>
    <PublishReadyToRun>False</PublishReadyToRun>
    <DebugType>None</DebugType>
  </PropertyGroup>
</Project>

If I set PublishReadyToRun to True (or add PublishTrimmed as True) it fails. Adding DebugType None prevents it publishing the pdb file for the exe.

UPDATE: removing the SelfContained tag stops it creating the 'dotnetcoreapp3.1' folder.


推荐阅读