I have been trying to used the following approach in my ASP.NET MVC project where Microsoft.AspNet.SignalR
library is used:
public interface ITypedHubClient
{
Task BroadcastMessage(string name, string message);
}
Inherit from Hub:
public class ChatHub : Hub<ITypedHubClient>
{
public void Send(string name, string message)
{
Clients.All.BroadcastMessage(name, message);
}
}
Inject your the typed hubcontext into your controller, and work with it:
public class DemoController : Controller
{
IHubContext<ChatHub, ITypedHubClient> _chatHubContext;
public DemoController(IHubContext<ChatHub, ITypedHubClient> chatHubContext)
{
_chatHubContext = chatHubContext;
}
public IEnumerable<string> Get()
{
_chatHubContext.Clients.All.BroadcastMessage("test", "test");
return new string[] { "value1", "value2" };
}
}
However, there is no IHubContext<THub,T>
Interface in Microsoft.AspNet.SignalR
library and I for this reason I cannot use IHubContext
with two parameters (IHubContext<ChatHub, ITypedHubClient> _chatHubContext;
). So, I am wondering if it is possible to a DI library or method. If so, how to fix this problem?
Microsoft.AspNetCore.SignalR.Core.dll
and namespace of the same name. – Navada