C# 如何获取HubProxy.Invoke调用的响应?
因此,我有一个C#C# 如何获取HubProxy.Invoke调用的响应?,c#,signalr,deadlock,signalr-hub,signalr-client,C#,Signalr,Deadlock,Signalr Hub,Signalr Client,因此,我有一个C#客户机,它调用SignalR集线器的方法,其中集线器将返回一个日期时间 但是我现在经历的行为是,客户端卡在HubProxy.Invoke上,它最终记录了以下内容: 检测到可能的死锁。使用“HubProxy.On”或“Connection.Received”注册的回调已执行至少10秒。 这是客户端的代码: private async Task<long> GetCurrentServerTimeOffset() { DateTime requestDate =
客户机
,它调用SignalR集线器
的方法
,其中集线器将返回一个日期时间
但是我现在经历的行为是,客户端
卡在HubProxy.Invoke上,它最终记录了以下内容:
检测到可能的死锁。使用“HubProxy.On”或“Connection.Received”注册的回调已执行至少10秒。
这是客户端的代码
:
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;
}
我已经尝试将wait-hubProxy.Invoke(“GetCurrentServerTime”)
替换为hubProxy.Invoke(“GetCurrentServerTime”)。结果
,但其行为相同
有人知道我做错了什么,导致致命警告被记录吗
EDIT1:如果我在返回日期时间中设置断点在集线器
的code>中,断点被命中,集线器
将其响应发送到客户端
这很奇怪,但您的代码在我的情况下运行良好。客户机/服务器配置可能有问题
我的简单服务器配置:
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;
}
}
客户端配置:
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;
}
}
这很奇怪,但你的代码在我的情况下运行得很好。客户机/服务器配置可能有问题
我的简单服务器配置:
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;
}
}
客户端配置:
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;
}
}
通过让它不被人伺候,我自己解决了它:
_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);
});
\u radioHubProxy.Invoke(“GetCurrentServerTime”).ContinueWith(response=>
{
DateTime serverDate=response.Result;
DateTime resultDate=DateTime.UtcNow;
长偏移量=serverDate.Ticks-(requestDate.Ticks+resultDate.Ticks)/2;
_mobi.SetServerTimeOffset(偏移量);
});
能够通过使其不被等待来自行修复:
_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);
});
\u radioHubProxy.Invoke(“GetCurrentServerTime”).ContinueWith(response=>
{
DateTime serverDate=response.Result;
DateTime resultDate=DateTime.UtcNow;
长偏移量=serverDate.Ticks-(requestDate.Ticks+resultDate.Ticks)/2;
_mobi.SetServerTimeOffset(偏移量);
});