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