I am trying to use a session in my Blazor Server app.
I am trying to add the variables to the Program.cs class, as startup doesn’t seem to exist anymore.
That means that nothing sits inside a namespace.
This is most of my program.cs file:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddServerSideBlazor().AddHubOptions(o =>
{
o.MaximumReceiveMessageSize = 10 * 1024 * 1024; // 10mb max file size
});
builder.Services.AddSyncfusionBlazor();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(60);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
var app = builder.Build();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.Run();
But this fails to start upon:
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
But only after I added the session part to it.
It fails with:
System.InvalidOperationException: "EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware by calling ‘IApplicationBuilder.UseRouting’ inside the call to ‘Configure(…)’ in the application startup code."
… which confuses me, as Configure() doesn’t exist anymore.
Can anyone help me out why my start up fails?
>Solution :
Beware that order of of calls in Program.cs matters.
Remove:
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
and after call app.UseRouting(); place this:
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");