Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor services need a specific order to be configured?

$
0
0

Today I found out that some configurations were not set while running the program.For simplification I will show you two examples, HubOptions and CookieAuthenticationOptions:

In the Program.cs where you configure your services:

var builder = WebApplication.CreateBuilder(args);// not working here://builder.Services.Configure<HubOptions>(options => { options.MaximumReceiveMessageSize = null; });// Add services to the container.builder.Services.AddRazorPages();builder.Services.AddServerSideBlazor();// builder.Services.AddServerSideBlazor().AddHubOptions(options => { options.MaximumReceiveMessageSize = null; }) does not work: // (HubOptions has to be configured at least after AddServerSideBlazor)builder.Services.AddSingleton<WeatherForecastService>();// working here:builder.Services.Configure<HubOptions>(options => { options.MaximumReceiveMessageSize = null; });builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>{    options.LoginPath = "/LoginPage";    options.Events.OnSigningIn = context => {        context.Properties.IsPersistent = true;        return Task.CompletedTask;    };    // not working:    options.ExpireTimeSpan = TimeSpan.FromMinutes(5);});// workingbuilder.Services.Configure<CookieAuthenticationOptions>(options => { options.ExpireTimeSpan = TimeSpan.FromMinutes(5); });var app = builder.Build();// ...

Testing what was configured in the Counter.razor page:

@inject IServiceProvider ServiceProvider// ...protected override void OnInitialized(){    base.OnInitialized();    var hubOptionsValue = ServiceProvider.GetRequiredService<IOptions<HubOptions>>()?.Value;    var cookieAuthenticationOptionsValue = ServiceProvider.GetRequiredService<IOptions<CookieAuthenticationOptions>>()?.Value;}

So it seems configuring the HubOptions by setting the MaximumReceiveMessageSize = null does not work before the AddServerSideBlazor() and when using AddHubOptions after it.

It is only set, when you use Configure<HubOptions>.

Same for the AddAuthentication configuration; when you configure it within it self by setting ExpireTimeSpan it will not be set and the default value will be used. ExpireTimeSpan will only be set when using Configure<CookieAuthenticationOptions>

Why is that?Is that a bug, is that normal and what would be the recommended way to configure the services?

Used the Blazor Server template with .Net Core 6, but found the same behaviour in .Net 7.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>