You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Config generator wrongly emits init-related exception in bind operations #90974
With the config generator, that same code throws an exception, because the generator emits this:
publicstaticvoidBind_Base(thisIConfigurationconfiguration,object?obj){thrownewInvalidOperationException("Cannot create instance of type '<global namespace>.Base' because it is missing a public instance constructor.");}
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
We have some options here, but handling polymorphism is a challenge in the source generator. I'm not sure if this should emit a diagnostic either. Assuming we don't do any polymorphic binding, what would you expect this to do:
Assuming we don't do any polymorphic binding, what would you expect this to do
Either a) not emit any code for this and raise a diagnostic that says as much just as happens in other cases where it can't fully handle the situation, or b) emit code that handles setting everything on Base, since that's no different from what it would do if Base weren't abstract. Silently emitting code that will always fail at execution time seems like the worst outcome.
b) emit code that handles setting everything on Base, since that's no different from what it would do if Base weren't abstract. Silently emitting code that will always fail at execution time seems like the worst outcome.
This is the best we could do, but I thought this was a bad idea since it isn't what happens at runtime today and instead would silently ignoring all of the properties on your derived type. What's worse? Null properties or an exception that says no polymorphism is supported?
b) emit code that handles setting everything on Base, since that's no different from what it would do if Base weren't abstract. Silently emitting code that will always fail at execution time seems like the worst outcome.
This is the best we could do, but I thought this was a bad idea since it isn't what happens at runtime today and instead would silently ignoring all of the properties on your derived type. What's worse? Null properties or an exception that says no polymorphism is supported?
Why is the abstract case handled differently from this?
changed the title [-]Config generator generates exceptions for things built-in Bind handles[/-][+]Config generator wrongly emits init-related exception in bind operations[/+]on Aug 23, 2023
Related issue of abstract/interface/polymorphic type handling shared by @adamsitnik in #91324.
I've tried to use the new configuration binder source generator to bind a type that contains a custom collection of abstract types.
I got following error:
C:\Users\adsitnik\source\repos\ConfigBindRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(61,29): error CS0
103: The name 'InitializeEndPoint' does not exist in the current context [C:\Users\adsitnik\source\repos\ConfigBindRepro\ConfigBindRepro.csproj]
I would expect it to not produce a compiler error and just try to map what it can, without throwing an exception at runtime (this is what the reflection based binder does in this case).
Activity
davidfowl commentedon Aug 23, 2023
We have some options here, but handling polymorphism is a challenge in the source generator. I'm not sure if this should emit a diagnostic either. Assuming we don't do any polymorphic binding, what would you expect this to do:
stephentoub commentedon Aug 23, 2023
Either a) not emit any code for this and raise a diagnostic that says as much just as happens in other cases where it can't fully handle the situation, or b) emit code that handles setting everything on Base, since that's no different from what it would do if Base weren't abstract. Silently emitting code that will always fail at execution time seems like the worst outcome.
davidfowl commentedon Aug 23, 2023
This is the best we could do, but I thought this was a bad idea since it isn't what happens at runtime today and instead would silently ignoring all of the properties on your derived type. What's worse? Null properties or an exception that says no polymorphism is supported?
stephentoub commentedon Aug 23, 2023
Why is the abstract case handled differently from this?
That's still polymorphic, the Value2 on Derived still won't be set with the generator, but there's no diagnostic and no exception.
davidfowl commentedon Aug 23, 2023
I didn't realize that the abstract base class changed the behavior. That is inconsistent... I would have expected both to behavior similarly....
layomia commentedon Aug 23, 2023
Taking a look, consistency is desired. We shouldn't throw here because we don't need to init the object.
IConfiguration.Getshould though.[-]Config generator generates exceptions for things built-in Bind handles[/-][+]Config generator wrongly emits init-related exception in bind operations[/+]layomia commentedon Sep 1, 2023
Related issue of abstract/interface/polymorphic type handling shared by @adamsitnik in #91324.
I've tried to use the new configuration binder source generator to bind a type that contains a custom collection of abstract types.
I got following error:
I would expect it to not produce a compiler error and just try to map what it can, without throwing an exception at runtime (this is what the reflection based binder does in this case).
Repro:
Details
layomia commentedon Sep 12, 2023
Needs servicing request for RC-2 backport.
ericstj commentedon Sep 15, 2023
I was trying out the fix here and noted a different scenario we might want to consider: #92137
ericstj commentedon Sep 20, 2023
Fixed in #92118