You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

System.Text.Json生成代码引发CI构建失败,求排查方案

System.Text.Json源代码生成在CI构建中出现重复定义错误

我正在使用System.Text.Json 6.0.5的源代码生成功能,代码如下:

[JsonSerializable(typeof(AuthenticationToken))]
[JsonSerializable(typeof(AuthenticationData))]
[JsonSerializable(typeof(ApplicationSettings))]
[JsonSerializable(typeof(Refit.ProblemDetails))]
// (...)
public partial class JsonContext : JsonSerializerContext
{
}

// (...)
private static JsonSerializerOptions GetDefaultOptions()
{
    var options = new JsonSerializerOptions();
    options.AddContext<JsonContext>();

    return options;
}

这段代码位于NetStandard2.0类库中,项目文件(csproj)除必要的项目和包引用外,仅包含以下属性配置:

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>9.0</LangVersion>
</PropertyGroup>

该代码在本地Visual Studio 2022和2019的Release与Debug配置下均可正常构建。

问题描述

当在CI流水线中构建相同代码时,出现大量重复定义相关的编译错误,部分错误信息如下:

##[error]C:\WIN1809-01\_work\11\s\src\app\ApplicationTemplate.Presentation\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.g.cs(10,6): Error CS0579: Duplicate 'global::System.CodeDom.Compiler.GeneratedCodeAttribute' attribute
##[error]C:\WIN1809-01\_work\11\s\src\app\ApplicationTemplate.Presentation\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.Type.g.cs(11,100): Error CS0102: The type 'JsonContext' already contains a definition for '_Type'
##[error]C:\WIN1809-01\_work\11\s\src\app\ApplicationTemplate.Presentation\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.Type.g.cs(12,98): Error CS0102: The type 'JsonContext' already contains a definition for 'Type'
##[error]C:\WIN1809-01\_work\11\s\src\app\ApplicationTemplate.Presentation\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.String.g.cs(11,102): Error CS0102: The type 'JsonContext' already contains a definition for '_String'
##[error]C:\WIN1809-01\_work\11\s\src\app\ApplicationTemplate.Presentation\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.String.g.cs(12,100): Error CS0102: The type 'JsonContext' already contains a definition for 'String'

实际错误约有200条,均来自自动生成的JsonContext.xxx.g.cs文件。

请问这是已知问题吗?我是否遗漏了某些配置?


内容的提问来源于stack exchange,提问作者Batesias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 05:54:18