Skip to content

.net 6 aspnet minimal api not using the System.Text.Json source generator? #45064

@NYMEZIDE

Description

@NYMEZIDE

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

.NET 6
aspnet web project with minimal api

When using the .NET 6.0 System.Text.Json source generator with minimal api, it seems that source generators files (*.g.cs) never used on runtime, on HTTP request-response jobs.

I put a lot of stopping points in the source generators files (*.g.cs), but code execution does not go there.

Expected Behavior

I'm testing the performance of minimal api against System.Text.Json without source generators and with it in the context of web api projects.

I get exactly the same query results per second (RPS) on a simple POST query example. (on running Release mode of course)

I don't understand how to make the code generated through JsonSerializerContext run.

Calling
var json = JsonSerializer.Serialize(new MyJsonDto() { }, MyJsonDtoContext.Default.MyJsonDto);
manually, in console app, successfully enters the *.g.cs files on Debug mode. And the benchmark shows an increase in performance in Release mode.

Steps To Reproduce

https://github.com/NYMEZIDE/JsonSourceGeneratorsNotUsed/

using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(options =>
{
    options.SerializerOptions.AddContext<MyJsonDtoContext>();
});

//builder.Services.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(options =>
//{
//    options.JsonSerializerOptions.AddContext<MyJsonDtoContext>();
//});


var app = builder.Build();

app.MapPost("/test", (MyJsonDto dto) =>
{
    return dto;
});

app.Run();



public class MyJsonDto
{
    public int Id { get; set; }

    public string Name { get; set; }
}

[JsonSerializable(typeof(MyJsonDto))]
public partial class MyJsonDtoContext : JsonSerializerContext { }

Exceptions (if any)

No response

.NET Version

6.0.400

Anything else?

.NET SDK (reflecting any global.json):
Version: 6.0.400
Commit: 7771abd614

Среда выполнения:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.400\

global.json file:
Not found

Host:
Version: 6.0.8
Architecture: x64
Commit: 55fb7ef977

.NET SDKs installed:
2.2.207 [C:\Program Files\dotnet\sdk]
6.0.400 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Download .NET:
https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
https://aka.ms/dotnet/runtimes-sdk-info

Metadata

Metadata

Assignees

No one assigned

    Labels

    old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions