如何获得HubProxy的响应.调用调用



所以我有一个C#client,它调用SignalRhubmethod,其中集线器将返回一个DateTime

但我现在经历的行为是,client卡在HubProxy.Invoke上,最终记录以下内容:

Possible deadlock detected. A callback registered with "HubProxy.On" or "Connection.Received" has been executing for at least 10 seconds.

这是client:的代码

private async Task<long> GetCurrentServerTimeOffset()
{
DateTime requestDate = DateTime.Now;
DateTime serverDate = await hubProxy.Invoke<DateTime>("GetCurrentServerTime");
DateTime resultDate = DateTime.Now;
long offset = serverDate.Ticks - (requestDate.Ticks + resultDate.Ticks) / 2;
return offset;
}

这是Hub:的代码

public DateTime GetCurrentServerTime()
{
return DateTime.Now;
}

我已经尝试用hubProxy.Invoke<DateTime>("GetCurrentServerTime").Result替换await hubProxy.Invoke<DateTime>("GetCurrentServerTime"),但它的行为相同。。。

有人知道我做错了什么,导致死亡警告被记录下来吗?

EDIT1:如果我在hubreturn DateTime.Now;中放置了一个断点,则该断点被命中,并且hub将其响应发送到client没有问题。

这很奇怪,但您的代码在我的情况下运行良好。也许客户端/服务器配置有问题?

我的简单服务器配置:

public partial class FormServer : Form
{
private IDisposable Server { get; set; }
private const string ServerURL = "http://localhost:8080";
public FormServer()
{
InitializeComponent();
}
private void ButtonStart_Click(object sender, EventArgs e)
{
Task.Run(StartServer); 
}
private void StartServer()
{
try
{
Server = WebApp.Start(ServerURL);
this.Invoke((Action)(() => buttonStart.Enabled = false)); 
consoleTextBox.Invoke((Action)(() => consoleTextBox.AppendText($"Server successfully started on {ServerURL} {Environment.NewLine}")));
}
catch (TargetInvocationException ex)
{
consoleTextBox.Invoke((Action)(() => consoleTextBox.AppendText($"Server failed to start. Error: {ex.Message} {Environment.NewLine}")));
return;
}
}
}

客户端配置:

public partial class FormClient : Form
{
private string ServerURL = "http://localhost:8080";
public HubConnection Connection { get; set; }
IHubProxy HubProxy { get; set; }
public FormClient()
{
InitializeComponent();
labelAddress.Text = ServerURL;
}
private void Connection_StateChanged(StateChange obj)
{
this.Invoke((Action)(() =>
{
labelState.Text = Connection.State.ToString();
if (Connection.State == ConnectionState.Disconnected) 
{
buttonConnect.Enabled = true;
}
}));
}
private async Task ConnectAsync()
{
Connection = new HubConnection(ServerURL);
HubProxy = Connection.CreateHubProxy("MyHub"); // Hub name
Connection.StateChanged += Connection_StateChanged;

try // try to connect to the server
{
await Connection.Start();
labelState.Text = Connection.State.ToString();
}
catch (HttpRequestException ex) // Catch an error
{
this.Invoke((Action)(() =>
{
richTextBox.AppendText($"Error: {Environment.NewLine} {ex.Message} {Environment.NewLine}");
}));
}
}
private async void ButtonConnect_Click(object sender, EventArgs e)
{
await ConnectAsync();
}
private async void MyButton_Click(object sender, EventArgs e)
{
long result = await GetCurrentServerTimeOffset();
MessageBox.Show(result.ToString());
}
private async Task<long> GetCurrentServerTimeOffset()
{
DateTime requestDate = DateTime.Now;
DateTime serverDate = await HubProxy.Invoke<DateTime>("GetCurrentServerTime");
DateTime resultDate = DateTime.Now;
long offset = serverDate.Ticks - (requestDate.Ticks + resultDate.Ticks) / 2;
return offset;
}
}

SignalR集线器:

public class MyHub : Hub
{
public DateTime GetCurrentServerTime() => DateTime.Now;
}

能够通过使其不被等待来自行修复:

_radioHubProxy.Invoke<DateTime>("GetCurrentServerTime").ContinueWith(response =>
{
DateTime serverDate = response.Result;
DateTime resultDate = DateTime.UtcNow;
long offset = serverDate.Ticks - (requestDate.Ticks + resultDate.Ticks) / 2;
_mobi.SetServerTimeOffset(offset);
});

最新更新