Blog Post

Signalr Cannot invoke a non-delegate type

Tuesday, March 22, 2016 2:12 PM

I have been working with signalr and I wanted to be able to send message from my HomeController

  

my code:

private IHubContext _hubContext;
 public HomeController()
{
  _hubContext = GlobalHost.ConnectionManager.GetHubContext<AmsHub>();
}
  public void SendMessage(){
 _hubContext.Clients.All().addMessage("home ctrl", "Sending message from action");
}

and then I have found an exception have been thrown after adding exception catching

 

 public void SendMessage()
{
try
{
_hubContext.Clients.All().addMessage("home ctrl", "Sending message from action");
}
catch (Exception ex)
{
var msg = ex.Message;
throw;
}
}

 

 Exception message:

Cannot invoke a non-delegate type

Resolution time

Turns out that that my code was not correct as accessing message and sending it should be done using following code

public void SendMessage()
{
try
{
_hubContext.Clients.All.addMessage("home ctrl", "Sending message from action");
}
catch (Exception ex)
{
var msg = ex.Message;
throw;
}
}

 

This is because all is not a method but more like property of all clients, and because it is DYNAMIC type it we do not have intellisence to rely on.

 

Hope that will help