-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Labels
area-Extensions-Optionsfeature-requestin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Milestone
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;
}
-->
Reactions are currently unavailable
Metadata
Metadata
Labels
area-Extensions-Optionsfeature-requestin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged