SignalR IHubProxy 接口是否公开任何指示客户端处于断开连接状态的属性?
Does the SignalR IHubProxy interface expose any properties that indicate that the client is in a disconnected state?
我有一个 .NET 客户端可以为我的集线器创建代理 class。当托管该集线器的服务器停机时间足够长时,客户端将进入断开连接状态。我想在调用集线器方法之前检查代理是否处于断开连接状态,而不是仅仅尝试调用集线器方法然后在它处于断开连接状态时捕获错误。
在 Visual Studio 中调试时,我可以看到 IHubProxy
对象有一个基础 属性 State
指示当前状态。正常工作时显示已连接,断开连接时显示已断开连接。但是,我似乎无法访问此 属性。
有谁知道有没有办法告诉?理想情况下,我只想做这样的事情:
if (hubProxy.State == ConnectionState.Disconnected)
{
this.AttemptReconnection();
}
if (hubProxy.State == ConnectionState.Connected)
{
await hubProxy.Invoke("MyMethod", myMethodArgs);
}
我意识到 HubConnection
class 是我想要的。我忘记了那个 class 因为我有一个代理服务 class 专门用于为集线器创建代理,我让它只公开它的 IHubProxy
属性 因为那是什么客户端用来调用集线器方法。通过公开其 HubConnection
属性,客户端也可以检查状态。
为了这个答案的完整性,这是我的客户端代码的骨架:
private void ConnectToHub()
{
try
{
// this is a method in the proxy service class that tries to connect to the hub
// it returns true if it was able to connect successfully
this.connected = hubProxyService.AttempConnectionToHub();
if (this.connected)
{
this.hubProxy = hubProxyService.HubProxy;
this.hubConnection = hubProxyService.HubConnection
}
}
catch
{
this.connected = false;
}
}
private void MyMethodThatInvokesHubMethod()
{
// Do some stuff
// ...
// ...
// ...
if (this.hubConnection.State == ConnectionState.Disconnected)
{
this.ConnectToHub();
}
if (this.hubConnection.State == ConnectionState.Connected)
{
await this.hubProxy.Invoke("MyHubMethod", hubMethodArgs);
}
}
我有一个 .NET 客户端可以为我的集线器创建代理 class。当托管该集线器的服务器停机时间足够长时,客户端将进入断开连接状态。我想在调用集线器方法之前检查代理是否处于断开连接状态,而不是仅仅尝试调用集线器方法然后在它处于断开连接状态时捕获错误。
在 Visual Studio 中调试时,我可以看到 IHubProxy
对象有一个基础 属性 State
指示当前状态。正常工作时显示已连接,断开连接时显示已断开连接。但是,我似乎无法访问此 属性。
有谁知道有没有办法告诉?理想情况下,我只想做这样的事情:
if (hubProxy.State == ConnectionState.Disconnected)
{
this.AttemptReconnection();
}
if (hubProxy.State == ConnectionState.Connected)
{
await hubProxy.Invoke("MyMethod", myMethodArgs);
}
我意识到 HubConnection
class 是我想要的。我忘记了那个 class 因为我有一个代理服务 class 专门用于为集线器创建代理,我让它只公开它的 IHubProxy
属性 因为那是什么客户端用来调用集线器方法。通过公开其 HubConnection
属性,客户端也可以检查状态。
为了这个答案的完整性,这是我的客户端代码的骨架:
private void ConnectToHub()
{
try
{
// this is a method in the proxy service class that tries to connect to the hub
// it returns true if it was able to connect successfully
this.connected = hubProxyService.AttempConnectionToHub();
if (this.connected)
{
this.hubProxy = hubProxyService.HubProxy;
this.hubConnection = hubProxyService.HubConnection
}
}
catch
{
this.connected = false;
}
}
private void MyMethodThatInvokesHubMethod()
{
// Do some stuff
// ...
// ...
// ...
if (this.hubConnection.State == ConnectionState.Disconnected)
{
this.ConnectToHub();
}
if (this.hubConnection.State == ConnectionState.Connected)
{
await this.hubProxy.Invoke("MyHubMethod", hubMethodArgs);
}
}