当我尝试使用 IHubProxy 调用集线器类方法时出现“连接尚未建立”的异常,但我无法找出问题所在。连接已成功,但以下代码行引发错误。
hubProxy.Invoke("SendNotificationToUser", new object[] { touser, message }).ContinueWith(task =>
{
if (task.IsFaulted && task.Exception != null)
{
// log error
}
});
这是我的完整代码..
[WebMethod]
public static void NotfTest(string message)
{
var hubConnection = new HubConnection("http://localhost:3052/CollegeBuilder/");
IHubProxy hubProxy = hubConnection.CreateHubProxy("NotificationHub");
var touser = "128";
try
{
lock (hubConnection)
{
if (hubConnection.State == Microsoft.AspNet.SignalR.Client.ConnectionState.Disconnected)
{
hubConnection.Start().Wait(2000);
}
}
hubProxy.Invoke("SendNotificationToUser", new object[] { touser, message }).ContinueWith(task =>
{
if (task.IsFaulted && task.Exception != null)
{
// log error
}
}); ;
}
finally
{
hubConnection.Stop();
}
}