我正在使用
WebApp.Start<Startup>(url)
在Windows服务中托管Signalr集线器。出于某些原因,我的集线器在运行2-3天后不接受连接,我如何检测到它的无响应并重新启动它?
答案 0 :(得分:0)
首先,您应该找到Windows服务崩溃的原因。编写事件日志,使用try catch
块,并将每个错误写入事件日志。
如何控制Windows服务:看看ServiceController类
添加类似方法:
public void StartService()
{
using (ServiceController service = new ServiceController(serviceName))
{
try
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
catch (Exception ex)
{
throw new Exception($"Can not Start the Windows Service [{serviceName}]", ex);
}
}
}
public void StartOrRestart()
{
if (IsRunningStatus)
RestartService();
else if (IsStoppedStatus)
StartService();
}
更新: 如果只有Hub遇到问题,请尝试从以下客户端启动它:
$.connection.hub.disconnected(function() {
setTimeout(function() {
$.connection.hub.start();
}, 5000); // Restart connection after 5 seconds.you can set the time based your requirement
});
随时添加带有您发布的更多信息的评论