In a Blazor WASM app under .NET 7 I configure standard Json serializer options like so in Program.cs
builder.Services.Configure<JsonSerializerOptions>(options => {
options.TypeInfoResolver = new PolymorphicAgentaDtoResolver();
options.PropertyNameCaseInsensitive = true;
});
Later on in a service of the App I want to serialize an object with JsonSerializer.Serialize(...)
. The result doesn't use the defaults from above. My question is why the defaults aren't used.
If I inject IOptions<JsonSerializerOptions>
via the constructor of the service and use them in the appropriate overload of JsonSerializer.Serialize(...)
the result is correct.
Code
public async Task<List<ServiceauftragDto>> GetServiceAuftraege(AgentaDto dto) {
string s = JsonSerializer.Serialize<AgentaDto>(dto); // wrong
s = JsonSerializer.Serialize(dto); // wrong
s = JsonSerializer.Serialize(dto, dto.GetType()); // wrong
s = JsonSerializer.Serialize(dto, dto.GetType(), injectedOptions.Value); // correct
s = JsonSerializer.Serialize(dto, JsonSerializerOptions.Default); // wrong
...
}