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
[Bug] Ignore properties when binding with Options service #58384
Is your feature request related to a problem? Please describe.
I'm using the Options service to bind hierarchical configuration data.
Suppose I have this class:
publicclassPersonOptions{publicstringName{get;set;}publicintAge{get;set;}publicboolFoo{// <--- I want binder to ignore thisget{// ... do stuff}}publicboolIgnoreMe=>thrownewException();// <--- I want binder to ignore this}
Because the Foo property is public, the binder runs it (which leads to various problems - why is it run?). My design requires it to be public, and I prefer not to change it to a method.
I hoped I could force the binder to ignore that property, e.g. using an attribute. I tried [JsonIgnore] but that didn't work.
Describe the solution you'd like
Please consider adding an attribute for this - if the property is decorated by that attribute, then don't try bind it, and don't run it.
Additional context
Thank you!
Reacted by Matt Munich, lonix1, Tobias Müller, Samuel, Simon Reitbauer, lexey-evergreen, Ben Pedersen, Scharly Ochoa, Will Nations, Abdul Mannan and 13 more
/// <summary>/// When false (the default), the binder will only attempt to set public properties./// If true, the binder will attempt to set all non read-only properties./// </summary>publicboolBindNonPublicProperties{get;set;}
This issue even looks more like a bug than a feature request, because a property with only a getter is read-only from the perspective of the binder.
The code behind the ConfigurationBinder changed quite some bit from 6 to 7, but I think based on these lines it'll still suffer from the same problem.
// If we don't have an instance, try to create oneif(bindingPoint.Valueisnull){// if the binding point doesn't let us set a new instance, there's nothing more we can doif(bindingPoint.IsReadOnly){return;}
It'll first try to access the bindingpoint value (which trys to resolve via the property getter) before it checks on the IsReadOnly. By doing the check earlier, the whole problem would resolve itself I think, because it wouldn't even read the value, when it's not going to set it anyways.
changed the title [-][Feature Request] Ignore properties when binding with Options service[/-][+][Bug] Ignore properties when binding with Options service[/+]on Sep 28, 2022
This is clearly a short-coming. A ConfigurationIgnoreAttribute should be added for sure; at least for the sake of parity considering ConfigurationKeyNameAttribute was implemented (see #36010).
Simple cases for supporting this cause:
backwards compatibility
post-load value conversion
post-load options derivatives
Example — my current issue due to the absence of such an attribute is the need to bind values specified in a nested IConfigurationSection into a pre-created object followed by additional processing of the loaded values and selection of some specific defaults (code simplified; in the real case the post-processing is more complicated):
/// <summary>
/// CSV ingestion options describing various CSV formats ingested (loaded) by the system.
/// </summary>
public class CsvIngestionOptions
{
/// <summary>
/// Default CSV format options.
/// </summary>
[ConfigurationKeyName("__ingored_"+nameof(DefaultFormat))] // BUG .NET has no attribute to ignore a particular property; see also https://github.com/dotnet/runtime/issues/58384
public CsvFormatOptions DefaultFormat { get; set; }
/// <summary>
/// Default CSV format options; for the sake of sparse JSON deserialization.
/// </summary>
[ConfigurationKeyName(nameof(DefaultFormat))]
public IConfigurationSection? DefaultFormatSection { get; set; }
/// <summary>
/// Invoked when the configuration is loaded from the file.
/// </summary>
internal void OnConfigurationLoaded()
{
// load default format, ensuring that at least the default encoding is set
DefaultFormat = new CsvFormatOptions();
if (DefaultFormatSection != null) DefaultFormatSection.Bind(DefaultFormat);
DefaultFormat.EnsureEncodingDefined();
}
}
Note: For the options being loaded from a JSON file, I would assume things as [JsonIgnore], [JsonPropertyName], and IJsonOnDeserialized to work naturally. However, they don't. This behavior deserves a mention in the docs, at least.
if(!propertyBindingPoint.IsReadOnly&&(propertyBindingPoint.Valueis not null||propertyBindingPoint.HasNewValue))
@crozone and others: Do you have read-only properties that are still being read by the binder? Or do you have read-write properties that you want to ignore using an attribute?
Triage: we see binder now ignores getter only properties so we won't make the IgnoreAttribute now. If anyone has a use-case that would benefit from the attribute, please make an issue focused directly on that.
Triage: we see binder now ignores getter only properties so we won't make the IgnoreAttribute now. If anyone has a use-case that would benefit from the attribute, please make an issue focused directly on that.
I see. Could you please make this a new issue? The problem is that this one is a bug report, whereas the attribute is really an API request, so it would attract more attention from the relevant people.
@rosebyte It's been a while since I've looked at this code. You've obviously looked at it recently.
Am I right in understanding that the bug (the binder running public properties) is resolved, but a "don't bind this" attribute is not yet implemented?
@lonix1 Yes, read-only properties are not read, but a "don't bind this" attribute doesn't exist. (We now have #125111 open for that, but we haven't talked about that yet.)
Activity
pfeigl commentedon Jul 15, 2022
Why has this been dropped from 7.0?
Looking at the
BinderOptionspropertyThis issue even looks more like a bug than a feature request, because a property with only a getter is
read-onlyfrom the perspective of the binder.The code behind the
ConfigurationBinderchanged quite some bit from 6 to 7, but I think based on these lines it'll still suffer from the same problem.It'll first try to access the bindingpoint value (which trys to resolve via the property getter) before it checks on the
IsReadOnly. By doing the check earlier, the whole problem would resolve itself I think, because it wouldn't even read the value, when it's not going to set it anyways.lonix1 commentedon Jul 16, 2022
Agreed, it doesn't work as expected, it's a bug. And I still don't understand why it runs my non-bindable properties.
If you have a better name for the issue so it gets more attention, let me know and I'll rename it.
tscbp commentedon Sep 22, 2022
Agreed, this is a bug - read-only properties (i.e. get-only) should not attempt to be bound by
Configure.turowicz commentedon Sep 28, 2022
They should use System.Text.Json to read the config files... would make it so much easier.
[-][Feature Request] Ignore properties when binding with Options service[/-][+][Bug] Ignore properties when binding with Options service[/+]ondrejtucny commentedon Jul 8, 2023
This is clearly a short-coming. A
ConfigurationIgnoreAttributeshould be added for sure; at least for the sake of parity consideringConfigurationKeyNameAttributewas implemented (see #36010).Simple cases for supporting this cause:
Example — my current issue due to the absence of such an attribute is the need to bind values specified in a nested
IConfigurationSectioninto a pre-created object followed by additional processing of the loaded values and selection of some specific defaults (code simplified; in the real case the post-processing is more complicated):Note: For the options being loaded from a JSON file, I would assume things as
[JsonIgnore],[JsonPropertyName], andIJsonOnDeserializedto work naturally. However, they don't. This behavior deserves a mention in the docs, at least.odyth commentedon Dec 7, 2023
The
[BindNever]attribute also doesn't work to suppress the all powerful binder from evaluating readonly properties as of .netcore 6crozone commentedon Mar 24, 2025
Any movement on this?
svick commentedon Mar 2, 2026
This seems to have been fixed in .Net 7 by #80438. The relevant code in
ConfigurationBindernow is:runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Line 312 in 1fd4dda
@crozone and others: Do you have read-only properties that are still being read by the binder? Or do you have read-write properties that you want to ignore using an attribute?
dotnet-policy-service commentedon Mar 2, 2026
This issue has been marked
needs-author-actionand may be missing some important information.rosebyte commentedon Mar 3, 2026
Triage: we see binder now ignores getter only properties so we won't make the IgnoreAttribute now. If anyone has a use-case that would benefit from the attribute, please make an issue focused directly on that.
ondrejtucny commentedon Mar 3, 2026
See my comment above for the actual use cases: #58384 (comment)
rosebyte commentedon Mar 3, 2026
I see. Could you please make this a new issue? The problem is that this one is a bug report, whereas the attribute is really an API request, so it would attract more attention from the relevant people.
lonix1 commentedon Mar 4, 2026
@rosebyte It's been a while since I've looked at this code. You've obviously looked at it recently.
Am I right in understanding that the bug (the binder running public properties) is resolved, but a "don't bind this" attribute is not yet implemented?
svick commentedon Mar 5, 2026
@lonix1 Yes, read-only properties are not read, but a "don't bind this" attribute doesn't exist. (We now have #125111 open for that, but we haven't talked about that yet.)
rosebyte commentedon Mar 5, 2026
@lonix1, yes, we have it on our radar but it will all be about priorities in the end.