-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configuration Binding source generator throws InvalidOperationException at runtime for type with custom [TypeConverter] use within a dictionary #93335
Comments
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsDescriptionIf the configuration binding source generator is used with a type that uses a custom For example, if this type is used for this configuration object in conjunction with a JSON configuration such as: {
"Site": {
"Keyed": {
"Somewhere": "0,0"
}
}
} The same issue does not seem to occur if the object is not within a dictionary. I have not tested the behavior when the object is a member of an array. Reproduction Steps
Expected behaviorThe configuration binding source generator produces code that is able to use the type converter to produce an instance of the given type from the Actual behaviorAn
Regression?Unknown compared to the previews and rc.1 - I had the configuration binding source generator disabled before RC2 due to #90851, #90987 and #91258 that prevented my code from running this far. Known WorkaroundsDisable the configuration binding source generator or refactor code to not use a custom Configuration.NET SDK Other informationNo response
|
This was intentionally out of scope for v1 of the configuration source generator. Please see #83599 for the way we're thinking about handling this as well as suggested workarounds. |
Fair enough - I did think it weird though that it doesn't work in a dictionary, but it seemed to work outside of one. However, I just tweaked my code a bit and had another look and in the case of it not being in a dictionary, it instead just silently doesn't bind anything at all and you get left with the default value, rather than an exception being thrown.
- "Keyed": {
- "Somewhere": "0,0"
- }
+ "Single": "1,2"
- _ = app.Services.GetRequiredService<IOptions<SiteOptions>>().Value;
+ var opts = app.Services.GetRequiredService<IOptions<SiteOptions>>().Value;
+ Console.WriteLine(opts.Single); Output:
|
Description
If the configuration binding source generator is used with a type that uses a custom
[TypeConverter]
to parse the type from a string, the generated code will throw anInvalidOperationException
at runtime because properties are not present in the configuration that map to the object's properties when the value is a value in a dictionary.For example, if this type is used for this configuration object in conjunction with a JSON configuration such as:
The same issue does not seem to occur if the object is not within a dictionary. I have not tested the behavior when the object is a member of an array.
Reproduction Steps
dotnet run --project .\Repro\
Expected behavior
The configuration binding source generator produces code that is able to use the type converter to produce an instance of the given type from the
string Value
property of theIConfiguration
passed to the generated method.Actual behavior
An
InvalidOperationException
is thrown the first time the application attempts to retrieve the bound configuration object.Regression?
Unknown compared to the previews and rc.1 - I had the configuration binding source generator disabled before RC2 due to #90851, #90987 and #91258 that prevented my code from running this far.
Known Workarounds
Disable the configuration binding source generator or refactor code to not use a custom
[TypeConverter]
.Configuration
.NET SDK
8.0.100-rc.2.23502.2
Other information
No response
The text was updated successfully, but these errors were encountered: