Is there a way to get AddOptionsWithValidateOnStart to work in Blazor Web Assembly? #62649
Replies: 1 comment
-
AddOptionsWithValidateOnStart can register the validation but there is one problem in WASM, it can't handles until you resolove the service yourself, you can fxi it using explicity calling host.Services.GetRequiredService<IOptions>() at startup. sing Microsoft.AspNetCore.Components.WebAssembly.Hosting; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.Services var host = builder.Build(); // force the options to resolve & validate here // if invalid, this will throw and prevent app from running await host.RunAsync(); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Given:
I am trying to validate the appsettings.json file of my client web project using AddOptionsWithValidateOnStart.
Here is the client project wwwroot/appsettings.json:
And FooSettings.cs:
And Program.cs:
I was expecting/hoping that when I run the application I get an exception complaining that the Bar setting was missing. Instead I can navigate around the Home/Counter/Weather pages without error.
If I inject FooSettings into a page, for example:
Then I do get the desired behaviour when I navigate to the Counter page. How I can I get this behaviour across the entire website?
Demo repo here: https://github.com/philipmorley/BlazorValidateOptions
Beta Was this translation helpful? Give feedback.
All reactions