question

SanyaAnishchik-6350 avatar image
0 Votes"
SanyaAnishchik-6350 asked SnehaAgrawal-MSFT commented

SignalR between mobile and PC

I have .Net Core server application with database that install locally on PC station. Now I need to develop mobile application that would connect to PC station and retrieve live reports based on database data.
Because PC station is placed under NAT with dynamic IP I cannot call API directly from mobile or via cloud with http request. I decided that Azure SignalR Service would be very helpful for such task.
I want to establish signalr connection MobileApp <---> Azure SignalR Core <---> Local PC API. Mobile sends request to the cloud and it transmit to PC API. For PC API I'm going to use BackgroundService to keep connection alive. Is it good workaround? Should I use HubConnectionBuilder in BackgroundService and create infinite loop? Where can I find how to use Signalr Client in BackgroundService?

dotnet-aspnet-core-webapiazure-signalr-service
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Just checking in to see if the below answers helped. If this answers your query, do click “Accept Answer” and Up-Vote for the same. And, if you have any further query do let us know.

1 Vote 1 ·
SnehaAgrawal-MSFT avatar image
1 Vote"
SnehaAgrawal-MSFT answered

Thanks for reply! It looks like you have a separate SignalR server and your desktop app is only a client. Then in this case you just use SignalR client to make the connection and send message. The code above looks all right. The only comment is instead of an infinite loop that does nothing you want to use Task.Delay.

Let us know.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SnehaAgrawal-MSFT avatar image
0 Votes"
SnehaAgrawal-MSFT answered

Thanks for asking question! We recommend to convert your server application to a SignalR server then it will be very easy to send messages to client.

If the “.NET Core server application” is already an ASP.NET Core server application you can just use SignalR server SDK. If not you need to convert it to an ASP.NET Core server first.

Let us know if further query on this on issue remains.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SanyaAnishchik-6350 avatar image
0 Votes"
SanyaAnishchik-6350 answered SanyaAnishchik-6350 edited

@SnehaAgrawal-MSFT thanks for reply! Do you mean to convert cloud server to SignalR application? My server app already SignalR Core application.
My Desktop API (Client of the server) uses HubConnectionBuilder in BackgroundService. I have next code to keep connection alive all time while application is running. Can you take a look and see if it good way to do such things?

  protected override async Task ExecuteAsync(CancellationToken stoppingToken)
         {
             while (!stoppingToken.IsCancellationRequested)
             {
                 using var scope = _serviceScopeFactory.CreateScope();
                 var orderHandler = scope.ServiceProvider.GetRequiredService<IReportHubHandler>();
                 var logger = scope.ServiceProvider.GetRequiredService<ILoggerFactory>()?
                     .CreateLogger(nameof(HubConnectionBackgroundService));
                 try
                 {
                     connection = new HubConnectionBuilder()
                             .WithUrl($"https://xxx.azurewebsites.net/zzz")
                             .WithAutomaticReconnect()
                             .Build();
                     connection.Closed += async (error) =>
                     {
                         logger.LogInformation(error, $"Connection closed {DateTime.Now}");
                         await Task.Delay(new Random().Next(0, 5) * 1000);
                         await connection.StartAsync(stoppingToken);
                         logger.LogInformation($"Connection started {DateTime.Now}");
                     };
                     orderHandler.SetHubConnection(connection);
                     connection.On<ReportDataRequest>("AAA", handler: orderHandler.AAA);
                     await connection.StartAsync(stoppingToken);
                     logger.LogInformation($"Connection started {DateTime.Now}");
                     while (true)
                     {
                     }
                 }
                 catch (Exception ex)
                 {
                     logger.LogError(ex, "Error in Hub Worker with Azure Signalr Service");
                     await Task.Delay(TimeSpan.FromMinutes(30), CancellationToken.None);
                 }
             }
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.