Guy
Guy

Reputation: 1512

Client cannot connect to SignalR hub of a Blazor WebAssembly app running on IIS hosting environment

I have created a Blazor WebAssembly Solution according to This Tutorial. The index page (code below, unchanged from tutorial):

  1. Works on VS2019 dev environment.
  2. On another PC I deploy to (machine2, Win10 pro), the page also works correctly on localhost:5000 (if I launch it ouside of IIS using the command dotnet AppName.dll)
  3. However on that PC (machine2), on IIS hosting the SignalR is not connecting (the page loads when connecting to the public URL, but the button to send a SignalR message is disabled, because IsConnected is false, and it REALLY IS DISCONNECTED).
  4. A ConsoleApp with identical code (see code at the bottom), has no problem connecting from machine1 to the public URL (on machine2), proving that the SignalR hub itself is running correctly.

What is causing this? How can I debug the client side?

@page "/"
@using Microsoft.AspNetCore.SignalR.Client
@inject NavigationManager NavigationManager

<div class="form-group">
    <label>
        User:
        <input @bind="_userInput" />
    </label>
</div>
<div class="form-group">
    <label>
        Message:
        <input @bind="_messageInput" size="50" />
    </label>
</div>
<button @onclick="Send" disabled="@(!IsConnected)">Send</button>

<hr>

<ul id="messagesList">
    @foreach (var message in _messages)
    {
    <li>@message</li>
    }
</ul>

@code {
    private HubConnection _hubConnection;
    private List<string> _messages = new List<string>();
    private string _userInput;
    private string _messageInput;

    protected override async Task OnInitializedAsync()
    {
        _hubConnection = new HubConnectionBuilder()
            .WithUrl(NavigationManager.ToAbsoluteUri("/chatHub"))
            .Build();

        _hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
        {
            var encodedMsg = $"{user}: {message}";
            _messages.Add(encodedMsg);
            StateHasChanged();
        });

        await _hubConnection.StartAsync();
    }

    Task Send() =>
        _hubConnection.SendAsync("SendMessage", _userInput, _messageInput);

    public bool IsConnected =>
        _hubConnection.State == HubConnectionState.Connected;
}

Below is a console app bare minimum that connects to the public signalR hub without any problems.

using System;
using Microsoft.AspNetCore.SignalR.Client;
namespace ConsoleSignalRDebug
{
    class Program
    {
        static private HubConnection _hubConnection;
        static void Main(string[] args)
        {
            try
            {
                _hubConnection = new HubConnectionBuilder()
                    .WithUrl("https://myblazorSite.com:443/chatHub")
                    .Build();
                _hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
                { Console.WriteLine("/////////////RECEIVED///////////////" + user + " " + message);});
                _hubConnection.StartAsync().GetAwaiter().GetResult();

                string x = Console.ReadLine();
                _hubConnection.SendAsync("SendMessage", x, x);
                Console.ReadLine();
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }
}

I tried this for debugging but it only works on the console (that works anyways):

_hubConnection = new HubConnectionBuilder()
                    .WithUrl("https://myblazorSite.com:443/chatHub")
                    .ConfigureLogging(logging =>
                    {
                    logging.SetMinimumLevel(LogLevel.Debug);
                    logging.AddConsole();
                        })
                    .Build();

Upvotes: 0

Views: 2709

Answers (1)

Quango
Quango

Reputation: 13468

WebSockets is enabled by default on IISExpress (used for local testing) but disabled by default on full-fat IIS.

So when deploying an application using WebSockets, such as SignalR (or server-side Blazor) you need to enable WebSockets on IIS.

The instructions for enabling IIS WebSockets are found at https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1#iisiis-express-support

Upvotes: 2

Related Questions