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, Func2 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With