Search code examples
blazorsignalr.net-8.0

How can I set or override Blazor default hub options .NET8


Does anyone know how I can override or set the default hub options in .NET 8? Looking at MS documentation, it seems that I should be able to just add the app.MapBlazorHub() line with options after app.MapRazorComponents in program.cs per ASP.NET Core Blazor SignalR guidance and Blazor hub options . When I try to add something like below

app.MapBlazorHub(options =>
{
    options.TransportSendTimeout = TimeSpan.FromMinutes(5);
});

I receive the following error about multiple endpoints being matched.

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request.

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:

Blazor initializers

at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(Span1 candidateState) at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, Span1 candidateState) at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, Span`1 candidateState) at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Does anyone know how I can override these options or the HttpConnectionDispatcherOptions for the default hub, _blazor, in .NET 8?

I can reproduce this by creating a new blank .NET 8 project and adding the above line.

I ask because I have these options overridden in a .NET6 project using the app.UseEndpoints with no problem

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllers();
    endpoints.MapBlazorHub(options =>
    {
        options.TransportSendTimeout = TimeSpan.FromMinutes(5);
    });
    endpoints.MapFallbackToPage("/_Host");
});

I tried using the same method I used previous in .NET6 and am experiencing the error I described in the post for ambiguous endpoints


Solution

  • In dotnet 8 Blazor you can configure HttpConnectionDispatcherOptions like this :

    app.MapRazorComponents<App>()
        .AddInteractiveServerRenderMode()
        .Add(convention =>
        {
            var meta = convention.Metadata.FirstOrDefault(e => e is HttpConnectionDispatcherOptions);
            if (meta is HttpConnectionDispatcherOptions options)
            {
                options.TransportSendTimeout = TimeSpan.FromMinutes(5);
            }
        });
    

    for CircuitOptions and HubOptions :

    builder.Services.AddRazorComponents().AddInteractiveServerComponents()
    .AddCircuitOptions(circuitOptions =>
    {
        circuitOptions.DisconnectedCircuitMaxRetained = 100;
    })
    .AddHubOptions(hubOptions =>
    {
        hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(10);
    });