SignalR .NET Client Invoke引发异常 [英] SignalR .NET Client Invoke throws an exception
问题描述
我正在尝试让一个与SignalR .Net Client一起使用的控制台应用程序,但是当我尝试在Hub上调用方法时却遇到了一个错误.这是我的控制台应用程序代码:
I'm trying to get a console application working with the SignalR .Net Client but I'm getting an error when I try to invoke a method on the Hub. Here's my code for the console app:
static void Main(string[] args)
{
var connection = new HubConnection("http://localhost/SignalRTest");
var myHub = connection.CreateProxy("SignalRTest.Classes.service");
myHub.On<string>("addMessage", text =>
{
Console.WriteLine(text);
});
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection: {0}", task.Exception.GetBaseException());
}
else {
Console.WriteLine("Connected.");
}
}).Wait();
myHub.Invoke("Send", "Message from console.").ContinueWith(task => {
if (task.IsFaulted)
{
Console.WriteLine("There was an error calling Send: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Send complete.");
}
});
Console.ReadLine();
}
这是来自服务器的集线器:
Here is the Hub from the Server:
[HubName("service")]
public class ServiceHub : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.addMessage(message);
}
}
我认为控制台应用程序正确连接,因为它写出已连接".但是,当它尝试在服务器上调用Send方法时,出现以下错误:
I assume that the console app is connecting correctly because it writes out "Connected." But when it tries to call the Send method on the server, I get the following error:
System.Net.WebException:远程服务器返回错误:(500)内部服务器错误.
在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
在SignalR.HttpHelper中.<> c_ DisplayClass2.b _0(IAsyncResult ar)
在System.Threading.Tasks.TaskFactory 1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endMethod,TaskCompletionSource`1 tcs)
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at SignalR.HttpHelper.<>c_DisplayClass2.b_0(IAsyncResult ar)
at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endMethod, TaskCompletionSource`1 tcs)
有人可以告诉我我在做什么错吗?谢谢.
Can anyone tell me what I'm doing wrong? Thanks.
推荐答案
此问题似乎是由使用无效的集线器名称引起的(使用CreateProxy时).奇怪的是start方法没有失败,但是我只是测试了一下,并得到了与您使用不存在的集线器名称相同的行为.
This issue appears to be caused by using an invalid hub name (when using CreateProxy). It is strange that the start method doesn't fail but i just tested this and got the same behavior as you using a non-existent hub name.
这篇关于SignalR .NET Client Invoke引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!