发布 .NET5 或更高版本时,不支持在单个文件包中包含符号

Including symbols in a single file bundle is not supported when publishing for .NET5 or higher

我尝试发布一个 .net Core (WinForms) 项目,我从 core3.0 升级到 net5.0,作为 Single Exe。

我收到以下错误:

NETSDK1142: Including symbols in a single file bundle is not supported when publishing for .NET5 or higher.

研究 NETSDK1142 我在 google 中没有得到任何关于修复的结果。

对于 Core3.0,以下命令有效:

dotnet publish nameofproject.csproj \
   -o bin/Release/core \
   -c Release \
   -p:Platform=x64 \
   -r win-x64 \
   --self-contained true

我还为 net5.0 尝试了以下标志。即使使用 DebugSymbols=false

仍然出现相同的错误
dotnet publish nameofproject.csproj \
    -o bin/Release/core \
    -c Release \
    -p:Platform=x64 \
    -p:NoWarn=CS0618 \
    -p:NoWarn=NU1605 \
    --self-contained true \
    -p:PublishSingleFile=true \
    -p:IncludeAllContentForSelfExtract=true \
    -p:PublishReadyToRun=true \
    -p:PublishTrimmed=false \
    -p:IncludeNativeLibrariesForSelfExtract=true \
    /p:DebugType=None /p:DebugSymbols=false

解决方案

只需在发布命令中添加-p:IncludeSymbolsInSingleFile=false/p:IncludeSymbolsInSingleFile=false

我是怎么找到它的

在 2 天后尝试解决问题后,在 dotnet/sdk 源代码中找到了解决方案。

sdk/src/Tasks/Common/Resources/Strings.resx中找到错误字符串

  <data name="CannotIncludeSymbolsInSingleFile" xml:space="preserve">
    <value>NETSDK1142: Including symbols in a single file bundle is not supported when publishing for .NET5 or higher.</value>
    <comment>{StrBegin="NETSDK1142: "}</comment>
  </data>

然后我在源代码中找到了CannotIncludeSymbolsInSingleFile字符串资源,在sdk/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets

中找到了如下结果
<NETSdkError Condition="'$(PublishSingleFile)' == 'true' And
                            '$(IncludeSymbolsInSingleFile)' == 'true' And
                            '$(_TargetFrameworkVersionWithoutV)' >= '5.0' And '$(TargetFrameworkIdentifier)' == '.NETCoreApp'"
                 ResourceName="CannotIncludeSymbolsInSingleFile" />

在那里我找到了我必须在构建时设置的标志 IncludeSymbolsInSingleFile