5

I have a problem in a blazor server application with websockets.

Using the application locally I have no problems, but when I load it on the hosting server I get these errors from the browser.

The application itself works correctly but occasionally some users get the message "Attempting to reconnect to the server".

websocket error

This is my Startup.cs

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

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // ******
        // BLAZOR COOKIE Auth Code (begin)
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => options.ExpireTimeSpan = TimeSpan.FromDays(1));

        // BLAZOR COOKIE Auth Code (end)
        // ******

        services.AddMudServices();

        services.AddRazorPages();
        services.AddServerSideBlazor();
       
        services.AddHttpContextAccessor();


        // ******
        // BLAZOR COOKIE Auth Code (begin)
        // HttpContextAccessor
        services.AddHttpContextAccessor();
        services.AddScoped<HttpContextAccessor>();
        services.AddHttpClient();
        services.AddScoped<HttpClient>();
        // BLAZOR COOKIE Auth Code (end)
        // ******
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        // ******
        // BLAZOR COOKIE Auth Code (begin)
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();
        // BLAZOR COOKIE Auth Code (end)
        // ******

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

What could be the problem ?

11
  • If it's contextual, it's likely a problem with your server configuration, not your app. Can you provide more details about that?
    – Connor Low
    Jun 8, 2021 at 15:32
  • If you're hosting it as a web app in Azure, make sure that on the web app's configuration, you've enabled the 'Use Websockets' options. Otherwise, the whole thing comes crashing down. - been there.
    – Dennis VW
    Jun 8, 2021 at 22:07
  • @Dennis1679 I am asking my host about the websockets settings because unfortunately I have very little control over the server. Jun 9, 2021 at 8:09
  • @ConnorLow My hoster said websockets are active. What else could it be ? Jun 9, 2021 at 13:50
  • Does your provider expose any debugging tools? Can you access any server logs?
    – Connor Low
    Jun 9, 2021 at 15:21

2 Answers 2

2

I got the same error. I did not have ARR Affinity turned on the Azure App Service.

Turning it on fixed the issue.

See: https://learn.microsoft.com/en-us/aspnet/core/signalr/publish-to-azure-web-app?view=aspnetcore-5.0#configure-the-app-in-azure-app-service

0

As I was just facing the same issue, maybe one additional note: I had the WebApp created manually in the Azure portal (not via the VS deployment) and therefor had no SignalR service connected at all.

This was working without websockets, but when trying to switch websockets "ON", the app wouldn't load anymore.

Now it works with SignalR Service added under "Service Connector" section of the web app.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.