Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SignalR client hangs when connecting with Long Polling transport; no errors in browser or server logs #119686

@swheat-starr

Description

@swheat-starr

Description

SignalR client configured to use only Long Polling does not connect. Client is a Blazor WebAssembly app. No errors appear in the browser console or server logs. Using ServerSentEvents or WebSockets also does not work.

The API server is running .NET 9 and exposes a SignalR hub at /messagehub. Direct navigation to /messagehub in the browser also hangs (as expected for a SignalR endpoint).

I have attempted to resolve this issue by interacting with GPT. I spent several hours and implemented all suggested fixes.

Reproduction Steps

  1. Create a .NET 9 API project with SignalR service registered and hub mapped:
app.UseRouting();
app.UseCors("AllowSpecificOrigin");
app.UseAuthorization();
app.UseEndpoints(endpoints => {
    endpoints.MapControllers();
    endpoints.MapHub<MessageHub>("/messagehub");
});
  1. Set up CORS to allow credentials and the client origin.
  2. Create Blazor WebAssembly client with connection code:
hubConnection = new HubConnectionBuilder()
    .WithUrl("https://localhost:7171/messagehub", options => options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.LongPolling)
    .Build();
await hubConnection.StartAsync();
  1. Run both projects and attempt to connect from the client.
  2. Observe that the connection hangs. No errors appear in browser console, and API logs show no errors.

API config:
net 9.0
Microsoft.AspNetCore.SignalR 1.2.0

Client config
net 9.0
Microsoft.AspNetCore.SignalR.Client 9.0.9

Client code:

public partial class MessageDialog
{
    [Inject]
    public NavigationManager Navigation { get; set; }

    #region Parameters
    [Parameter]
    public bool Show { get; set; } = false;

    [Parameter]
    public CancellationToken CancellationToken { get; set; }

    [Parameter]
    public EventCallback OnClose { get; set; }

    #endregion

    private HubConnection? hubConnection;
    private List<string> messages = new();

    protected override async Task OnInitializedAsync()
    {

        string hubURI = "https://localhost:7171/messagehub";
        hubConnection = new HubConnectionBuilder()
            .WithUrl(hubURI, options => options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.LongPolling)
            .Build();

        try
        {
            await hubConnection.StartAsync();
        }
        catch (Exception ex)
        {
            string y = ex.ToString();
        }

        hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
        {
              // this code is never executed
            messages.Add($"{user}: {message}");
            StateHasChanged();
        });


        hubConnection.On<string>("ReceiveMessage", (message) =>
        {
              // this code is never executed
            messages.Add(message);
            InvokeAsync(StateHasChanged);
        });

    }

    private async Task OnCloseHandlerAsync()
    {
        await OnClose.InvokeAsync();
    }
}

Server code:

// hub
public class MessageHub : Hub
{
}

// service

// inject
IHubContext<MessageHub> messageHub

// send message
await _messageHub.Clients.User(userName).SendAsync("ReceiveMessage",  "Message...");
await _messageHub.Clients.All.SendAsync("ReceiveMessage", "Message...");

Expected behavior

The SignalR client should connect to the hub using Long Polling, with requests/responses visible in the browser network tab and on the server. Messages should be sent/received normally.

Actual behavior

The client hangs when attempting to connect. No error messages appear in the browser console. API server logs show no errors. No WebSocket traffic is observed (as expected for Long Polling), only HTTP requests that do not complete the connection.

Regression?

Not sure if this is a regression; I have not tested previous builds or releases.

Known Workarounds

No known workaround.

Configuration

API: .NET 9
Client: Blazor WebAssembly
Operating System: Windows 10 x64
Browser: Edge/Chrome
SignalR: Microsoft.AspNetCore.SignalR
CORS: Allowed client origin and credentials
Hub mapped at /messagehub
No authentication required.

Other information

Is there any known issue in .NET/SignalR that could cause the client connection to hang with Long Polling and no errors? Are there additional diagnostics or configuration steps to expose why the connection does not complete?

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-wasmWebAssembly architectureneeds-area-labelAn area label is needed to ensure this gets routed to the appropriate area ownersneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsiderationos-browserBrowser variant of arch-wasmquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions