Skip to content

[WIP] Fix IServiceProvider resolution for DataAnnotations validation#125651

Draft
Copilot wants to merge 1 commit intomainfrom
copilot/fix-validate-dataannotations-options
Draft

[WIP] Fix IServiceProvider resolution for DataAnnotations validation#125651
Copilot wants to merge 1 commit intomainfrom
copilot/fix-validate-dataannotations-options

Conversation

Copy link
Contributor

Copilot AI commented Mar 17, 2026

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>Wire up IServiceProvider when validating DataAnnotations attributes on Options</issue_title>
<issue_description>### Describe the bug
App was unable to resolve IWebHostEnvironment from ValidationContext when validating options with custom Required attribute inside Configure method.

To Reproduce

IWebHostEnvironment not resolving, when trying to create a custom validation attribute to make something required only in Production environment.

Relevant code snippets

    public class DemoOptions
    {
        [RequiredInProd]
        public string FooBar { get; set; }
    }

    public class RequiredInProd : RequiredAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            //this errors
            var env = validationContext.GetRequiredService<IWebHostEnvironment>();
            return ValidationResult.Success;
        }
    }
//Startup.cs methods
public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions<DemoOptions>()
                .Bind(Configuration.GetSection(nameof(DemoOptions)))
                .ValidateDataAnnotations();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //this line works fine and IWebHostEnvironment is resolved.
            var envFromServices = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
            //this errors when trying to resolve IWebHostEnvironment inside Attribute methods
            _ = app.ApplicationServices.GetRequiredService<IOptions<DemoOptions>>().Value;
        }

-->

</issue_description>

<agent_instructions>propose a PR for this issue, make it a draft, focus on correctness, we don't want to break anything</agent_instructions>

Comments on the Issue (you are @copilot in this section)


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wire up IServiceProvider when validating DataAnnotations attributes on Options

2 participants