Repository navigation
Change ConfigurationBinder to avoid reading properties unnecessarily. - #66723
Conversation
Previously, ConfigurationBinder would eagerly read properties it encountered and initialize/bind objects for them even when those reads/initializations could lead to thrown exceptions or unnecessary work. Now, we defer work until we determine that it will actually contribute to the final binding result. Fix dotnet#52905 Fix dotnet#65710
| { | ||
| Type genericType = typeof(List<>).MakeGenericType(type.GenericTypeArguments[0]); | ||
| object? instance = Activator.CreateInstance(genericType); | ||
| object instance = Activator.CreateInstance(genericType)!; |
There was a problem hiding this comment.
Calling Activator.CreateInstance on a List can't ever return null, right?
There was a problem hiding this comment.
It is unlikely to return null for List. I am seeing we didn't check for Dictionary<,> too in the next method.
There was a problem hiding this comment.
That's what I thought; just wanted to highlight the change.
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsPreviously, ConfigurationBinder would eagerly read properties it
|
| } | ||
|
|
||
| if (instance is null | ||
| && Nullable.GetUnderlyingType(type) is { } nullableUnderlyingType) |
There was a problem hiding this comment.
This is technically new functionality. Previously, null mutable structs that were initially null would end up nooping in BindNonScalar. In my mind it makes more sense to treat these the same whether they start as null or not.
There was a problem hiding this comment.
This makes sense to me, but I think we should wrap any exceptions thrown from Activator.CreateInstance(nullableUnderlyingType).
try
{
if (Nullable.GetUnderlyingType(type) is { } nullableUnderlyingType)
{
// Activator.CreateInstance only returns null for Nullable<T> types.
return Activator.CreateInstance(nullableUnderlyingType)!;
}
return Activator.CreateInstance(type)!;
}
catch (Exception ex)
{
throw new InvalidOperationException(SR.Format(SR.Error_FailedToActivate, type), ex);
}If we don't believe Activator.CreateInstance only returns null for Nullable<T> types, we should be checking the return values for both calls to Activator.CreateInstance to see if they're null.
There was a problem hiding this comment.
I think we should wrap any exceptions thrown from Activator.CreateInstance(nullableUnderlyingType).
Good call. I forgot that this can happen with default struct constructors.
If we don't believe Activator.CreateInstance only returns null for Nullable types, we should be checking the return values for both calls to Activator.CreateInstance to see if they're null.
@halter73 I'll defer to you on this. I vaguely recall reading a C# article years ago that said some COM objects could result in null returns from Activator.CreateInstance. No idea whether that's still true in .NET Core, whether we care if it is, or whether there are other edge cases like that to consider. Unfortunately, I can't find the original source. If we think explicitly handling Nullable<T> is sufficient to guarantee a non-null return, we can just use ! as in your example.
There was a problem hiding this comment.
Should we do the null check anyway to be in the safe side?
I'm also unsure if Activator.CreateInstance can return null in other cases. It's the only scenario I see where the docs say CreateInstance will return null, but I think null checking all created instances as the PR currently does (except for with well-known types like List<T>) makes sense.
# Conflicts: # src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
|
Thanks for the PR @madelson, the changes at first glance seem fine, thanks the test cases look good too. Note for reviewers, /cc @halter73 would be good to get your review on this as well. |
|
Just to note, if flipping the order of the properties, this should workaround this issue: public class Configuration
{
public IEnumerable<int> ConfigValues { get; set; } = null!;
public IEnumerable<int> FilteredConfigValues => ConfigValues.Where(p => p > 10);
} |
Address feedback from dotnet#66723 (comment) and from dotnet#66723 (comment).
@maryamariyan I added a new test case explicitly reproducing this issue. |
tarekgh
left a comment
There was a problem hiding this comment.
LGTM. Thanks @madelson!
@halter73 @davidfowl are you interested to take a look at this change?
halter73
left a comment
There was a problem hiding this comment.
LGTM aside from the two minor comments.
…ConfigurationBinderTests.cs Review feedback from dotnet#66723 (comment) Co-authored-by: Stephen Halter <[email protected]>
|
@tarekgh I've applied the changes. One very small question to be resolved here: #66723 (comment) |
|
Thanks @madelson for helping with this PR. |
Previously, ConfigurationBinder would eagerly read properties it
encountered and initialize/bind objects for them even when those
reads/initializations could lead to thrown exceptions or unnecessary
work. Now, we defer work until we determine that it will actually
contribute to the final binding result.
Fix #52905
Fix #65710