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);
}
}
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: 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.
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.
You have not added the
SignalR.hosting.AspNet
dll, so this error is created. So, first include the dll withSignalR.Hosting.AspNet
on the server.For me this was a result of my SignalR hub requiring a dependency injection parameter. The error gets quite well hidden, even with the ShowDetailedErrors being set to true it was obscured except within the Server logs of Azure.
The error you get is something like this:
As you can see I was using the 'DefaultHubActivator' which doesn't know how to manage injector parameters. So I followed this fine advice which taught me how to do the injection using Unity.
The basics are you need to write your own hub activator, but you can inherit from the default one and just extend it to avoid having to register the dozens of dependencies that SignalR has.
After which you set it up in your Startup
Finally registering your hub with Unity