Blazor SignalR setup fails

Peter Annandale 21 Reputation points
2022-12-20T03:35:19.253+00:00

Hi,
Hopefully someone can point me in the right direction to resolve this problem, I have setup another SignalR hub in my blazor Server application and am getting the following failure when tryign to access the page that the SignalR hub is supposed to be connected to for auto updates.

System.Net.Http.HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).  
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()  
   at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync(Uri url, HttpClient httpClient, ILogger logger, CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync(Uri uri, CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport(TransferFormat transferFormat, CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore(TransferFormat transferFormat, CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync(TransferFormat transferFormat, CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore(CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner(CancellationToken cancellationToken)  
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync(CancellationToken cancellationToken)  
   at RevIntra.UI.Base.Pages.Revroof.RevbendOrderListViewPage.OnInitializedAsync() in D:\Shared\Source\Repos\Revroof\RevIntra_BP\RevIntra\Shared\RevIntra.UI.Base\Pages\Revroof\RevbendOrderListViewPage.cs:line 75  
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()  
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)  

My code kicks off form the follwing..

 protected override async Task OnInitializedAsync()  
    {  
        userInfo = await ((IdentityAuthenticationStateProvider)authStateProvider).GetUserViewModel();  
          
        _hubConnection = new HubConnectionBuilder()  
            .WithUrl(NavigationManager.ToAbsoluteUri("/revbendorderhub"))  
            .WithAutomaticReconnect()  
            .Build();  
          
        _hubConnection.On<AXOrderHeader>("ReceiveMessage", (orderHeader) =>  
        {  
            int index = this._messages.FindIndex(x => x.OrderDid == orderHeader.OrderDid);  
            if (index >= 0)  
            {  
                if (orderHeader.OpenOrder)  
                {  
                    this._messages[index] = orderHeader;  
                }  
                else  
                {  
                    this._messages.RemoveAt(index);  
                }  
            }  
            else if (orderHeader.OpenOrder)  
            {  
                this._messages.Add(orderHeader);  
            }  
              
            StateHasChanged();  
        });  
          
        await _hubConnection.StartAsync(); **<--- Call to intialise hub**  
        loadCompleted = true;  
    }  

The module that it is failing in is the inbuilt HubConnection class at this point:

private async Task StartAsyncInner(CancellationToken cancellationToken = default)  
    {  
        await _state.WaitConnectionLockAsync(token: cancellationToken).ConfigureAwait(false);  
        try  
        {  
            if (!_state.TryChangeState(HubConnectionState.Disconnected, HubConnectionState.Connecting))  
            {  
                throw new InvalidOperationException($"The {nameof(HubConnection)} cannot be started if it is not in the {nameof(HubConnectionState.Disconnected)} state.");  
            }  
  
            // The StopCts is canceled at the start of StopAsync should be reset every time the connection finishes stopping.  
            // If this token is currently canceled, it means that StartAsync was called while StopAsync was still running.  
            if (_state.StopCts.Token.IsCancellationRequested)  
            {  
                throw new InvalidOperationException($"The {nameof(HubConnection)} cannot be started while {nameof(StopAsync)} is running.");  
            }  
  
            using (CancellationTokenUtils.CreateLinkedToken(cancellationToken, _state.StopCts.Token, out var linkedToken))  
            {  
                await StartAsyncCore(linkedToken).ConfigureAwait(false); <-- **here is where it is failing**  
            }  
  
            _state.ChangeState(HubConnectionState.Connecting, HubConnectionState.Connected);  
        }  
        catch  
        {  
            if (_state.TryChangeState(HubConnectionState.Connecting, HubConnectionState.Disconnected))  
            {  
                _state.StopCts = new CancellationTokenSource();  
            }  
  
            throw;  
        }  
        finally  
        {  
            _state.ReleaseConnectionLock();  
        }  
    }  

Regards
Peter

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,592 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,500 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Jim Donnelly 6 Reputation points
    2022-12-23T15:30:14.997+00:00

    In the Application Pool Advanced Settings - The Identity User password had expired. Also, changed the Enable 32- bit Applications to True.

    This fixed my issue

    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 66,056 Reputation points
    2022-12-20T22:11:48.46+00:00

    did you create the hub in program.cs?

    app.MapHub<MyHub>("/revbendorderhub");

    0 comments No comments

  3. Jim Donnelly 6 Reputation points
    2022-12-22T20:37:09.09+00:00

    I'm getting the same issue:
    500, Internal Server Error at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync(Uri url, HttpClient httpClient, ILogger logger, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync(Uri uri, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport(TransferFormat transferFormat, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore(TransferFormat transferFormat, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync(TransferFormat transferFormat, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken) at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore(CancellationToken cancellationToken) at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner(CancellationToken cancellationToken) at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync(CancellationToken cancellationToken) at ETR.Blazor.Client.Services.HubConnectionService.HubConnectionService.StartConnectionAsync()

    I believe I'm configured OK, because it runs ok on my local dev box.

    This only occurs when I publish to the test servers..

    0 comments No comments

  4. Hamza Mouddakir 0 Reputation points
    2023-06-24T10:57:28.3966667+00:00

    I have the same problem only when i hosted on IIS in default website but when i hosted as speared application work without error but real-time not working

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.