我有一个C#SignalR客户端,并且希望在连接到我的服务器时成功/失败时执行一些操作。这里是我的代码:即使连接不可能,SignalR Start()的任务仍在继续
this.connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
this.OnRaiseServerConnectionClosedEvent();
}
else
{
this.JoinGroup();
this.StopTimer();
this.OnRaiseServerConnectionOpenedEvent();
}
});
}
else块总是执行,不关心,如果一台服务器在不在......
我也试图与坐等或与等待(),但同样的情况。
我明白.net任务正确,我认为但在这里我卡住了。
您的帮助将不胜感激:)
晏
编辑:
现在我的代码看起来像
try
{
this.connection.Start().Wait();
if (this.connection.State == ConnectionState.Connected)
{
this.JoinGroup();
this.StopTimer();
this.OnRaiseServerConnectionOpenedEvent();
}
}
catch (AggregateException)
{
this.OnRaiseServerConnectionClosedEvent();
}
catch (InvalidOperationException)
{
this.OnRaiseServerConnectionClosedEvent();
}
如果没有服务器存在,任务的创建Start()方法返回时没有错误并且连接状态。如果您想要执行某些操作或重试连接,则必须检查连接的状态。
您正在运行什么版本的SignalR服务器和.Net客户端? – halter73
对不起,应该提到它。服务器和客户端在v2.0中。 –