I am trying to call a method in the signalr Hub class from an (ASP.NET Core) MVC Controller, but I cannot find an example online that shows how to.
Note: There are lots of examples using older versions of signalr with the .Net Framework, but none that I can see that show how to do this in .Net Core.
I need to pass an id from the an MVC Action Result directly through to my Hub, without passing the id to the page, and then having to get a client connection back through to the hub.
public class ChatHub : Hub
{
public async Task DoSomething(int id)
{
//// Something in here.
}
}
public class HomeController : Controller
{
private readonly IHubContext<ChatHub> _hubContext;
public HomeController(IHubContext<ChatHub> hubContext)
{
_hubContext = hubContext;
}
public async Task<ActionResult> Index(int id)
{
//// Call the DoSomething method from here, passing the id across.
await _hubContext.Clients.All.SendAsync("AddToGroup", groupId);
}
}
Is there a way to do this please? (Or am I looking at this the wrong way, and is there a better way to achieve the same result?)
Update: If I pass the Id into the view, and then use JavaScript to call the Hub, this then calls the DoSomething method, so I can see it all hangs together correctly, but not when I try to call it in C#.