C# – SignalR message not being received on the client

csignalrwcfwpf

I've been trying to get my WPF client app to receive a SignalR message sent by the WCF service. I've tried many things and have now resorted to hacking away in the hopes that something just works. I've followed tutorials and examples online, and I simply can't get my WPF OnSignalRMessage() method to get called. Where am I going wrong here?

My hub:

public class PrestoHub : Hub
{
    public void Send(string message)
    {
        Clients.All.OnSignalRMessage(message);
    }
}

My startup class:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HubConfiguration { EnableCrossDomain = true };

        app.MapHubs("http://localhost:8084", config);
    }
}

The method that starts my SignalR host (within my WCF service host):

    private void StartSignalRHost()
    {
        const string url = "http://localhost:8084";
        WebApplication.Start<Startup>(url);
    }

The code to actually send some message:

GlobalHost.ConnectionManager.GetHubContext<PrestoHub>().Clients.All.OnSignalRMessage("snuh");
Console.WriteLine("Sent 'snuh' to all clients...");

My WPF client methods:

    private void InitializeSignalR()
    {
        var hubConnection = new Connection("http://localhost:8084");
        hubConnection.Start();
        hubConnection.Received += OnSignalRMessage;
    }

    private void OnSignalRMessage(string data)
    {
        MessageBox.Show(data);
    }

Best Answer

While I'm still struggling to understand the how and why, I was able to get it working. +1 to N. Taylor Mullen for pointing me in the right direction. In addition to his suggestion on the client side, I had to change some server code as well, namely using an empty hub and a simplified Startup class.

My hub:

public class PrestoHub : Hub{}

Note: The hub is empty because we're not calling methods within it. As we'll see later, we get the hub context and send messages to the clients.

My startup class:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapHubs();
    }
}

The above code seems to be what fixed the problem. This also works:

var config = new HubConfiguration { EnableCrossDomain = true };
app.MapHubs(config);

But as soon as I specify a URL, my client doesn't receive the messages (tried with and without the "SignalR" part):

app.MapHubs("http://localhost:8084/SignalR", config);

The method that starts my SignalR host (within my WCF service host):

private void StartSignalRHost()
{
    const string url = "http://localhost:8084";
    WebApplication.Start<Startup>(url);
}

The code to actually send some message:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<PrestoHub>();
hubContext.Clients.All.OnSignalRMessage("snuh");

My WPF client method:

private void InitializeSignalR()
{
    var hubConnection = new HubConnection("http://localhost:8084");
    var prestoHubProxy = hubConnection.CreateHubProxy("PrestoHub");
    prestoHubProxy.On<string>("OnSignalRMessage", (data) =>
        {
            MessageBox.Show(data);
        });
    hubConnection.Start();
}
Related Topic