SignalR client-server connection issues


grey rhino

I've been trying to figure out the problem all day and can't solve it. Here is my SignalR hub ( BlazorServerAppHub.cs )

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.SignalR;
    using BlazorServerApp.Data;
    
    namespace BlazorServerApp
    {
        public class BlazorServerAppHub: Hub
        {
            public const string HubUrl = "/chat";
      
            public async Task Broadcast(WeatherForecast[] forecasts)
            {
                await Clients.All.SendAsync("ReceiveMessage", forecasts);
            }
    
            public override Task OnConnectedAsync()
            {
                return base.OnConnectedAsync();
            }
        }
    }

This is the business logic page ( WeatherForecastService.cs )

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using BlazorServerApp.Data;
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Timers;

namespace BlazorServerApp.Data
{
    public class WeatherForecastService  
    {
        private readonly IHubContext<BlazorServerAppHub> _hubContext;

        public WeatherForecastService(IHubContext<BlazorServerAppHub> hubContext)
        {
            _hubContext = hubContext;
        }
        
        protected  async Task Broadcast()
        {
            var forecasts = await GetForecastAsync();
            await _hubContext.Clients.All.SendAsync("Broadcast", forecasts);
        }

        public Task<WeatherForecast[]> GetForecastAsync()
        {
            var rng = new Random();
        }    
                
    }

 public class WeatherForecast
    {
        public int A { get; set; }

    }
}

Finally, here is my razor page ( FetchData.razor )

@page "/fetchdata"

@using BlazorServerApp.Data;
@inject NavigationManager navigationManager
@using Microsoft.AspNetCore.SignalR.Client;

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

@if (forecasts == null)
{
    <p><em>Loading...</em></p>
}
else
{

    <table class="table">
        <thead>
            <tr>
                <th>A</th>
               
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in forecasts)
            {
                <tr>
                    <td>@forecast.A</td>
                    
                </tr>
            }
        </tbody>
    </table>
}

@code {

    private  WeatherForecast[] forecasts;
    private HubConnection _hubConnection;

    protected override async Task OnInitializedAsync()
    {
        string baseUri = navigationManager.BaseUri;
        string hubUrl = baseUri.TrimEnd('/') + BlazorServerAppHub.HubUrl;

        _hubConnection = new HubConnectionBuilder().WithUrl(hubUrl).Build();
        _hubConnection.On< WeatherForecast[]>("Broadcast", forcast => { forecasts = forcast; StateHasChanged(); });

        await _hubConnection.StartAsync();
    }
}

Basically, I'm trying to get data from the server and push it to the client periodically. The problem is that when I run the app forecastsin FetchData.razor the page is empty, so the page says "Loading". why is that? My guess is that I am missing something in the SignalR communication.

devNull

In order for the server to periodically push data to the client, you need to run some kind of background service. There are multiple ways to do this, for example:

However, since you said you only want to send data once for testing, you can hook into sending data to clients Hub.OnConnectedAsync()when they initially connect . First, I recommend creating a separate interface/repository for Task<WeatherForecast[]> GetForecastAsync():

public interface IWeatherForecastRepository
{
    Task<WeatherForecast[]> GetForecastAsync();
}

public class WeatherForecastRepository
{
    private static readonly string[] Tickers = new[]
    {
        "10", "20", "30", "44", "77"
    };

    private static readonly int[] Ones = new[]
    {
        1000, 15000, 7000, 500, 2200
    };

    public Task<WeatherForecast[]> GetForecastAsync()
    {
        var rng = new Random();
        return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            A = index,
            B = Tickers[index - 1],
            C = NextFloat(rng),
            D = Ones[index - 1],
            E = rng.Next(0, 10000),
        }).ToArray());
    }

    static float NextFloat(Random random)
    {
        double decimalPart = random.NextDouble();
        double intPart = random.Next(0, 1000);

        return (float)Math.Round(intPart + decimalPart, 3); ;
    }
}

Then, when you register that repository with DI, you can inject it into your own library BlazorServerAppHuband use OnConnectedAsync():

public class BlazorServerAppHub : Hub
{
    public const string HubUrl = "/chat";

    private readonly IWeatherForecastRepository _weatherForecastRepository;

    public BlazorServerAppHub(IWeatherForecastRepository weatherForecastRepository)
    {
        _weatherForecastRepository = weatherForecastRepository;
    }

    public async Task Broadcast(WeatherForecast[] forecasts)
    {
        await Clients.All.SendAsync("ReceiveMessage", forecasts);
    }

    public override async Task OnConnectedAsync()
    {
        await base.OnConnectedAsync();

        var forecasts = await _weatherForecastRepository.GetForecastAsync();

        // Clients.Caller will only send the data to the client that just connected
        await Clients.Caller.SendAsync("Broadcast", forecasts);
    }
}

Related


SignalR client-server connection issues

grey rhino I've been trying to figure out the problem all day and can't solve it. Here is my SignalR hub ( BlazorServerAppHub.cs ) using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Micros

SignalR client-server connection issues

grey rhino I've been trying to figure out the problem all day and can't solve it. Here is my SignalR hub ( BlazorServerAppHub.cs ) using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Micros

SignalR client-server connection issues

grey rhino I've been trying to figure out the problem all day and can't solve it. Here is my SignalR hub ( BlazorServerAppHub.cs ) using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Micros

SignalR client-server connection issues

grey rhino I've been trying to figure out the problem all day and can't solve it. Here is my SignalR hub ( BlazorServerAppHub.cs ) using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Micros

SignalR client-server connection issues

grey rhino I've been trying to figure out the problem all day and can't solve it. Here is my SignalR hub ( BlazorServerAppHub.cs ) using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Micros

SignalR client-server connection issues

grey rhino I've been trying to figure out the problem all day and can't solve it. Here is my SignalR hub ( BlazorServerAppHub.cs ) using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Micros

API server as SignalR client

Pascal R. I have an ASP.NET MVC Core application served by a web server. The web server has a SignalR hub and gets its data from a dedicated API server. Is it possible to register a web client and API server as SignalR clients so that the API server can push d

Keep SignalR client connection alive

John Livermore If my SignalR client connection drops, I want the client to try to reconnect. Is this a good pattern to achieve this? I'm referring to handling the Closed event on the SignalR connection by restarting the connection. public class OnPremiseWebHub

SignalR .Net client connection limit

development system I have .net client on Windows Universal App and SignalR Server hosted on localhost Web API, IIS Windows 10, Client application installed on Windows 10, when initiating a client connection, the second client connection start method hangs, if

SQL Server connection issues

gap I have the following statement in a stored procedure that returns strange results. Given two columns, one of which (RL) is less than 0, say -2, then 2 should be added to the other column (HD). If the negative value is -8, 8 should be added to the HD column

SQL Server connection issues

Leo Mujaj I am developing a program in C# and WPF. I wish to store data in a SQL Server database. I created a connection string on the PC with the instance name and it worked. However, when I want to use the IP address to connect over the Internet, I get some