1
votes

I'm trying to run a signalR hub method in one project from another project in the same solution. Both projects run on the same web front end.

When calling the following code, the code runs in the hub, up to the point where it expectantly errors due to not using an IHubContext object.

presentation.ChatHub ch = new ChatHub();
ch.Send(message, "", "");

When that happened, I changed my code to the following, however the code inside the hub no longer runs.

IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
context.Clients.All.Send(message, "", "");

The hub code is called as follows:

public class ChatHub : Hub
{
    public int Send(string message, string fName, string IName)
    {
    //Code Goes here.
    }

Everything I have read including other answers on here seem to point to getting the IhubContext then calling clients.all should work, yet I have no idea why it is not working

EDIT: i've changed my code so that a post is created in the database prior to running, and changed it so that it runs the 'broadcastMessage' function, which matches the javascript receiving end. yet that still doesn't wire up properly and javascript function is not hit.

   IHubContext context = GlobalHost.ConnectionManager.GetHubContext("chatHub");
   context.Clients.All.broadcastMessage(message, pid, userName, FirstName, count, "", "", nDate);
1

1 Answers

2
votes

I'm not 100% on this, but based on my understanding on the following page: ASP.Net SignalR Hubs API It is saying that when you call a method from outside the hub in, you aren't calling the method you have defined on the hub, instead you are telling the hub to activate the jquery on all of the connected clients.

This is why you aren't seeing your hub method activating.