Azure-webjobs-sdk: WebJobsOptionsFactory implementation ignores the "validations" parameter

Created on 18 Jun 2020  路  5Comments  路  Source: Azure/azure-webjobs-sdk

Because the implementation of the WebJobsOptionsFactory ignores the validation, it does not perform the intended validation.

Repro steps

public class Startup : FunctionsStartup
{
    public Startup()
    {
        var config = new ConfigurationBuilder()
            .AddEnvironmentVariables();

        Configuration = config.Build();
    }

    public IConfiguration Configuration { get; }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddOptions<SampleOptions>()
               .Bind(Configuration.GetSection("Sample"))
               .ValidateDataAnnotations();
    }
}

public class SampleOptions
{
    [Required]
    public string Value { get; set; }
}

Expected behavior

I expect the validation to run.
Sample:Value key if the application settings are not set and OptionsValidationException is thrown.

Actual behavior

No validation is performed and the property values are not checked.

Known workarounds

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        // Replace default implementation
        builder.Services.Replace(ServiceDescriptor.Transient(typeof(IOptionsFactory<>), typeof(OptionsFactory<>)));
    }
}

Related information

https://github.com/Azure/azure-webjobs-sdk/blob/f386e54e5000ddddf255f529689ab2a9c3ee78cd/src/Microsoft.Azure.WebJobs.Host/Hosting/OptionsFormatter/WebJobsOptionsFactory.cs#L28

bug

Most helpful comment

@fabiocav Any update on this? I was trying to validate options for azure functions v3 with no luck

All 5 comments

Assigning this to the upcoming sprint

Moving to sprint 86, but we're trying to get this into the 85 payload.

@fabiocav Any update on this? I was trying to validate options for azure functions v3 with no luck

@selMicrosoft Let me follow up with @soninaren on this issue.

Assigning this to @brettsam as he was recently impacted by the issue and worked on a fix.

Was this page helpful?
0 / 5 - 0 ratings