Hubconnection does not update data received from signalr server

48 Views Asked by At

On server I use background service + SignalR hub to send time to all clients. Client-side is written using Winforms.

The server is sending correct data to client: (check log)

      Worker running at: 08/16/2023 14:44:29
info: Pr.WebApi.HostedServices.TimeHostedService[0]
      Worker running at: 08/16/2023 14:44:30
info: Pr.WebApi.HostedServices.TimeHostedService[0]
      Worker running at: 08/16/2023 14:44:31
info: Pr.WebApi.HostedServices.TimeHostedService[0]
      Worker running at: 08/16/2023 14:44:32
info: Pr.WebApi.HostedServices.TimeHostedService[0]
      Worker running at: 08/16/2023 14:44:33
info: Pr.WebApi.HostedServices.TimeHostedService[0]
      Worker running at: 08/16/2023 14:44:34
info: Pr.WebApi.HostedServices.TimeHostedService[0]
      Worker running at: 08/16/2023 14:44:35
info: Pr.WebApi.HostedServices.TimeHostedService[0]
      Worker running at: 08/16/2023 14:44:36
info: Pr.WebApi.HostedServices.TimeHostedService[0]
      Worker running at: 08/16/2023 14:44:37

The output of client-side is the next (I started it at 2:43:17):

8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM
8/16/2023 2:43:17 PM

My client-side code for this part:

private async void ClientForm_Load(object sender, EventArgs e)
{
     await Task.Run(() => ExecuteAsync());
}

public async Task ExecuteAsync()
{
    while (true)
    {
        try
        {
            if (connection.State != HubConnectionState.Connected)
            {
                await connection.StartAsync();

                ConnectionStatusLabel.Invoke(delegate
                        {
                            ConnectionStatusLabel.Text = "Connected";
                            ConnectionStatusLabel.ForeColor = Color.FromArgb(0, 225, 50);
                        });
            }

            connection.On<DateTime>("SendTime", e =>
                    {
                        TimeShowLabel.Invoke(delegate
                        {
                            TimeShowLabel.Text = e.ToString("HH:mm:ss");
                            Debug.WriteLine(e.ToString());
                        });
                    });
        }
        catch (Exception ex)
        {
            ConnectionStatusLabel.Invoke(delegate
                    {
                        ConnectionStatusLabel.Text = "Not connected";
                        ConnectionStatusLabel.ForeColor = Color.Red;
                    });
            Console.WriteLine(ex.Message);
            Thread.Sleep(1000);
        }
    }
}

If I will move invocation of await connection.StartAsync(); outside the if(connection.State != HubConnectionState.Connected) block, the output will be as expected, but an exception will be thrown:

Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
Pr.WebApi.HostedServices.TimeHostedService: Information: Worker running at: 08/16/2023 14:48:32

Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
Pr.WebApi.HostedServices.TimeHostedService: Information: Worker running at: 08/16/2023 14:48:33

Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll

Thanks a lot.

I tried to work around connection to hub, placing it in different part of code.

I am expecting the code to be working in a proper way without throwing any exception and updating the time correctly.

0

There are 0 best solutions below