Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options data annotations validation not working in Blazor apps #30565

Closed
bgillet opened this issue Mar 2, 2021 · 3 comments
Closed

Options data annotations validation not working in Blazor apps #30565

bgillet opened this issue Mar 2, 2021 · 3 comments

Comments

@bgillet
Copy link

bgillet commented Mar 2, 2021

Here is a sample options class.

using System.ComponentModel.DataAnnotations;

namespace SampleApp.Options
{
    public class SampleOptions
    {
        public const string ConfigSectionName = "Sample";

        [Required(AllowEmptyStrings = false)]
        [RegularExpression(pattern: @"^[0-9]{14,14}$")]
        public string Dummy { get; set; }
    }
}

Below is code used in Startup class.

using SampleApp.Options;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace SampleApp
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions<SampleOptions>()
                    .Bind(Configuration.GetSection(SampleOptions.ConfigSectionName))
                    .ValidateDataAnnotations();

            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddSingleton<WeatherForecastService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}

When executing the code, I would expect an OptionsValidationException being thrown on application startup if I have bad option values set in my appsetting.json file.

But when I start my Blazor server application, nothing happens !!!

@davidfowl
Copy link
Member

davidfowl commented Mar 2, 2021

Options validation doesn't happen at startup but that feature has been added in .NET 6. See dotnet/runtime#36391 for more information.

@bgillet
Copy link
Author

bgillet commented Mar 2, 2021

Ok. Thanks. But when does it happen then ? On DI ?

This limitation should be clearly explained in documentation. It seems a very important to know.

@davidfowl
Copy link
Member

Ok. Thanks. But when does it happen then ? On DI ?

It happens on host startup as part of an IHostedService implementation.

This limitation should be clearly explained in documentation. It seems a very important to know.

Agreed, can you file an issue on this topic https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0#options-validation.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants