Build fails because of System.Text.Json generated code

Viewed 72

I'm using the source generation from System.Text.Json (version 6.0.5).

My code looks like this.

[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;
}

This code is in a NetStandard2.0 library. Apart from the project and package references, the csproj only contains the following properties.

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

This code works locally in both Release and Debug configuration and with both Visual Studio 2022 and 2019.

Problem

When I build the same code in my CI pipeline, it yields the following errors.

##[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'

These are just the first few lines. There are actually ~200 lines of similar errors, all originating from JsonContext.xxx.g.cs generated files.

Is this a known problem? Am I just missing some config?

1 Answers

Solution

The assembly containing the JsonContext declaration needs to have a direct reference to System.Text.Json. It cannot be a transitive reference.


In my case, I have multiple assemblies representing the applications layers:

DAL -> Business -> Presentation

I move my code (the JsonContext declaration) from the Presentation assembly (which relies on the DAL assembly for the System.Text.Json reference) to the DAL assembly (which has the System.Text.Json reference) and it works now.

Related