Skip to content

[Bug] Ignore properties when binding with Options service #58384

Closed as not planned

Description

@lonix1

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:

public class PersonOptions
{

  public string Name { get; set; }

  public int Age { get; set; }

  public bool Foo {                                // <--- I want binder to ignore this
    get {
      // ... do stuff
    }
  }

  public bool IgnoreMe => throw new Exception();   // <--- 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!

Activity

  1. ghost added
    untriaged
    on Aug 30, 2021
  2. removed
    untriaged
    on Aug 31, 2021
  3. added this to the 7.0.0 milestone on Aug 31, 2021
  4. modified the milestones: 7.0.0, Future on Jul 14, 2022
  5. pfeigl commented on Jul 15, 2022

    @pfeigl

    Why has this been dropped from 7.0?

    Looking at the BinderOptions property

            /// <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>
            public bool BindNonPublicProperties { 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 one
                    if (bindingPoint.Value is null)
                    {
                        // if the binding point doesn't let us set a new instance, there's nothing more we can do
                        if (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.

  6. lonix1 commented on Jul 16, 2022

    @lonix1
    Author

    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.

  7. tscbp commented on Sep 22, 2022

    @tscbp

    Agreed, this is a bug - read-only properties (i.e. get-only) should not attempt to be bound by Configure.

  8. turowicz commented on Sep 28, 2022

    @turowicz

    They should use System.Text.Json to read the config files... would make it so much easier.

  9. changed the title [-][Feature Request] Ignore properties when binding with Options service[/-] [+][Bug] Ignore properties when binding with Options service[/+] on Sep 28, 2022
  10. ondrejtucny commented on Jul 8, 2023

    @ondrejtucny

    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.

  11. odyth commented on Dec 7, 2023

    @odyth
    Contributor

    The [BindNever] attribute also doesn't work to suppress the all powerful binder from evaluating readonly properties as of .netcore 6

  12. crozone commented on Mar 24, 2025

    @crozone

    Any movement on this?

  13. svick commented on Mar 2, 2026

    @svick
    Member

    This seems to have been fixed in .Net 7 by #80438. The relevant code in ConfigurationBinder now is:

    if (!propertyBindingPoint.IsReadOnly && (propertyBindingPoint.Value is 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?

  14. added
    needs-author-action
    on Mar 2, 2026
  15. dotnet-policy-service commented on Mar 2, 2026

    @dotnet-policy-service
    Contributor

    This issue has been marked needs-author-action and may be missing some important information.

  16. modified the milestones: Future, 11.0.0 on Mar 3, 2026
  17. rosebyte commented on Mar 3, 2026

    @rosebyte
    Member

    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.

  18. ondrejtucny commented on Mar 3, 2026

    @ondrejtucny

    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.

    See my comment above for the actual use cases: #58384 (comment)

  19. rosebyte commented on Mar 3, 2026

    @rosebyte
    Member

    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.

  20. lonix1 commented on Mar 4, 2026

    @lonix1
    Author

    @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?

  21. svick commented on Mar 5, 2026

    @svick
    Member

    @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.)

  22. rosebyte commented on Mar 5, 2026

    @rosebyte
    Member

    @lonix1, yes, we have it on our radar but it will all be about priorities in the end.

  23. locked and limited conversation to collaborators on Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Configurationfeature-requestneeds-author-action

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions