In my application I have a Background Service whit overridden StartAsync method to connect to a VOIP server for listening to its events continuously.
This application is hosted on IIS 10 (Server1) and VOIP is on Centos7 (Server2).
The problem is StartAsync method not working until I browse website inside IIS, also it seems to be stopped about an hour later and I have to browse website again to make it start!
As a tip, this application is supposed to be a SignalR hub so clients can connect to it and get related events for further operations.
Is this a correct approach for listen to another server events as a background service? Or it could be implemented under better solution?
public class PBXEventListener : BackgroundService
{
private static AriClient ActionClient = new AriClient(
new StasisEndpoint("10.3.3.1", 8088, "asterisk", "asterisk"),
"HelloWorld", true);
private readonly IHubContext<AsteriskHub> _asterHub;
private readonly IServiceProvider _service;
private readonly IConfiguration _configuration;
private ConcurrentDictionary<string, string> _deviceStates = new ConcurrentDictionary<string, string>();
public PBXEventListener(IHubContext<AsteriskHub> asterHub, IServiceProvider service, IConfiguration configuration)
{
_asterHub = asterHub;
_service = service;
_configuration = configuration;
}
public override Task StartAsync(CancellationToken stoppingToken)
{
return Task.Run(() =>
{
using (var scope = _service.CreateScope())
{
var callStatRepo = scope.ServiceProvider.GetRequiredService<ICallStatRepository>();
_deviceStates = callStatRepo.GetAndSetLastStates();
}
ActionClient.OnConnectionStateChanged += ActionClientOnConnectionStateChanged;
ActionClient.OnDialEvent += ActionClientOnDialEvent;
ActionClient.OnChannelStateChangeEvent += ActionClientChannelStateChangeEvent;
ActionClient.OnDeviceStateChangedEvent += ActionClientDeviceStateChangeEvent;
ActionClient.Connect();
});
}
}