Код клиента Signalr вызывает проблему производительности Windows Сервис. Windows Сервис выступает в роли клиента-сигнализатора.
Что мы пытаемся сделать:
Windows служба имеет один таймер, который выполняет метод ( ConnectToSignalRServer ). Если каким-либо образом соединение закрывается, возникает одно событие ( Connection.Closed + = Connection_Closed ), которое снова попытается установить sh соединение, вызвав метод ( ConnectToSignalRServer ) , Пока в событии используется l oop ( Connection.Closed + = Connection_Closed ), чтобы попытаться восстановить соединение.
Пожалуйста, найдите приведенный ниже пример кода и сообщите мне, если возникнут какие-либо проблемы с код.
private static HubConnection Connection = null;
//When the service starts, this method would be called.
public static bool ConnectToSignalRServer()
{
try
{
string Url = "http://www.samplesignalrserver.com";
Connection = new HubConnection(Url);
var myHub = Connection.CreateHubProxy("SignalHub");
Connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
}
else
{
Connection.Closed += Connection_Closed;
}
}).Wait();
//Method(RequestData) would be called upon receiveng message from server
myHub.On<string>("GetMessgeFromServer", type =>
{
Task.Run(() => RequestData(type));
});
//Method(GetHostName) would be called in server
myHub.Invoke<string>("GetHostName", BLConstants.strHostName);
return true;
}
catch (Exception ex)
{
//capturing Stacktrace and Message from ex object
}
return false;
}
//Establish the connection, if the the connection would be closed
private static void Connection_Closed()
{
try
{
while (true)
{
Thread.Sleep(10000);
bool connected = ConnectToSignalRServer();
if (connected)
{
break;
}
}
}
catch (Exception ex)
{
//capturing Stacktrace and Message from ex object
}
}