Scott
Scott

Reputation: 13921

SignalR not calling client methods

So I've read all of the other SO questions with regard to this but it looks like everything should be set up properly. I'm running a self-hosted SignalR console application that has a single empty Hub declaration (this is for one-way communication from the server to connected clients).

EDIT: Link to project which reproduces the issue - https://dl.dropboxusercontent.com/u/66477739/SignalRTest.zip

The demo solution has 3 projects:

PublishStatusHub.cs

public class PublishStatusHub : Hub
{
}

JS

$(document).ready(function () {
    $.connection.hub.url = "http://localhost:8080/signalr";

    var publish = $.connection.publishStatusHub;

    publish.client.addMessage = function (message) {
        console.log(message);
    };

    $.connection.hub.start().done(function() { });
});

Calling code from a separate assembly

var context = GlobalHost.ConnectionManager.GetHubContext<PublishStatusHub>();
context.Clients.All.addMessage("Starting publish...");

Although I can see that the above code is executed (via breakpoints), the client method is never called.

I did turn on SignalR logging and no exceptions are being thrown. Also, it's never logging the incoming messages.

Interestingly enough, I can get messages sent to the client through my Main() method in Program.cs within the self-hosted console app:

Program.cs

static void Main(string[] args)
{
    string url = "http://localhost:8080";
    using (WebApp.Start(url))
    {
        Console.WriteLine("Server running on {0}", url);

        while (true)
        {
            string message = Console.ReadLine();

            IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<PublishStatusHub>();
            hubContext.Clients.All.addMessage(message);
        }
    }
}

Now, if I type in the console window and hit enter, the message is sent successfully to the client and it's logged in the console output window.

HOWEVER, if I move this code to another file within the same console application and then call it from another assembly (which is the end goal), it fails silently again:

MessagePublisher.cs

public class MessagePublisher
{
    public void Publish(string message)
    {
        IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<PublishStatusHub>();
        hubContext.Clients.All.addMessage(message);
    }
}

Calling code from a separate assembly

var messagePublisher = new MessagePublisher();
messagePublisher.Publish("Test message");

I'm stuck on why I can't publish messages to connected clients from external assemblies.

Upvotes: 0

Views: 1064

Answers (2)

bravohex
bravohex

Reputation: 1034

I guess you've created a Constructor, you could remove it...

Upvotes: 0

Evk
Evk

Reputation: 101443

In the project you posted, you are trying to publish a message from completely different console application (SignalRTest.BusinessLogic). This application does not host SignalR service, and so has no idea about connected clients. All clients are connected to the service hosted by another application (SinglaRTest.SelfHost) in another process. So there is no way how this could work. If you will start your service in BusinessLogic application, then this will work as usual.

Upvotes: 2

Related Questions