HttpClientException when connecting to working hub from SignalR .NET Client

Darren picture Darren · May 28, 2015 · Viewed 12.9k times · Source

I have a working SignalR application that allows me to connect multiple JavaScript clients and exchange data. When I tried to connect with a .NET client I get the following error:

An exception of type 'Microsoft.AspNet.SignalR.Client.HttpClientException' occurred in mscorlib.dll but was not handled in user code

Additional information: StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

{
  Transfer-Encoding: chunked
  X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcRGFycmVuXERlc2t0b3BcQ29uc29sZUFwcGxpY2F0aW9uMVxXZWJBcHBsaWNhdGlvbjFcc2lnbmFsclxuZWdvdGlhdGU=?=
  Cache-Control: private
  Date: Thu, 28 May 2015 09:13:06 GMT
  Server: Microsoft-IIS/8.0
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Type: text/html; charset=utf-8
}

To remove as many variables as possible I copied the Hub into an brand new web application and copied the .NET client code into a console application. I still get the same exception. Here is my client code:

Dim hubConnection As HubConnection
Dim chatHubProxy As IHubProxy

Public Async Sub RunTest()
    System.Net.ServicePointManager.DefaultConnectionLimit = 10

    hubConnection = New HubConnection("http://localhost:64400")
    hubConnection.Credentials = Net.CredentialCache.DefaultCredentials
    hubConnection.TraceLevel = TraceLevels.All
    hubConnection.TraceWriter = Console.Out

    chatHubProxy = hubConnection.CreateHubProxy("Chat")

    AddHandler hubConnection.StateChanged, Sub(stateChange) Console.WriteLine("[" & DateTime.Now & "]: " & stateChange.OldState.ToString() & " =>  " & stateChange.NewState.ToString() & " " & hubConnection.ConnectionId)

    chatHubProxy.On(Of String, String)("ReceiveMessage", Sub(from, message) Console.WriteLine(message))
    Await hubConnection.Start()
End Sub

Here is the console output:

09:21:54.3952161 - null - ChangeState(Disconnected, Connecting)
[28/05/2015 10:21:54]: Disconnected =>  Connecting
[28/05/2015 10:21:56]: Connecting =>  Disconnected
09:21:56.8448452 - null - Disconnected
09:21:56.8458461 - null - Transport.Dispose()
09:21:56.8468465 - null - Closed

And here is my hub code:

public class ChatHub : Hub
{
    public void SendMessage(string name, string message)
    {
        Clients.All.ReceiveMessage(name, message);
    }
}

Answer

Darren picture Darren · May 28, 2015

This turned out to be a trivial mistake, but the error message was so useless I'm sure others will be stumped by the same issue. The name of the hub was wrong. I used "Chat" when I should have used "ChatHub".

If the exception had been 404, or "Hub not found" or something like that it would have been an easy fix rather than a couple of wasted hours!