Configuration binding fails silently when JSON property names use camelCase while C# properties use PascalCase without proper attribute mapping in .NET 6+ minimal hosting model
0 reputation · 20 Aug 2022, 01:22 UTC
0 reputation · 20 Aug 2022, 01:22 UTC
The .NET configuration system relies on matching JSON property names to C# property names during options binding. When using IOptions<T> with AddOptions().BindConfiguration(), the default binding behavior expects exact case-sensitive matches or uses the configured naming policy. Without explicit [JsonPropertyName] attributes or a consistent naming policy applied via AddOptions().Configure(), camelCase JSON properties fail to bind to PascalCase C# properties silently, resulting in default values being used instead.
This behavior becomes more pronounced in the minimal hosting model where configuration binding occurs during service registration via ConfigureServices(), and there is no compile-time validation to catch mismatched property names. The absence of binding errors makes debugging difficult, especially when migrating from appsettings.json files that follow standard camelCase conventions.
Is this silent failure behavior documented for .NET 6+ minimal hosting model, and what is the recommended approach to ensure reliable configuration binding between JSON and strongly-typed options classes?
Should developers always apply [JsonPropertyName] attributes or configure a consistent naming policy like System.Text.Json.JsonNamingPolicy.CamelCase when using IOptions binding in minimal APIs?
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.