SignalR between mobile and PC

Sanya Anishchik 41 Reputation points
2021-07-23T10:29:12.35+00:00

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?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,553 questions
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
142 questions
{count} votes

Accepted answer
  1. SnehaAgrawal-MSFT 21,506 Reputation points
    2021-07-29T04:23:07.757+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. SnehaAgrawal-MSFT 21,506 Reputation points
    2021-07-27T04:33:42.607+00:00

    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.

    0 comments No comments

  2. Sanya Anishchik 41 Reputation points
    2021-07-28T10:33:34.973+00:00

    @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);  
                    }  
                }  
            }  
    
    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.