1

所以我有一个 C#client调用methodSignalR 的 a hub,集线器将在其中返回 a 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;
}

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

有谁知道我做错了什么,导致致命警告被记录?

EDIT1:return DateTime.Now;如果我在中放置断点hub,则断点被命中,并且hub将其响应发送到client.

4

2 回答 2

0

能够通过使其不等待来自己修复它:

_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);
});
于 2020-08-12T08:32:50.873 回答
0

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

我的简单服务器配置:

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;
}
于 2020-08-11T12:05:48.560 回答