SignalR : How to use IHubContext Interface in ASP.NET MVC?

前端 未结 1 1687
孤城傲影
孤城傲影 2021-01-17 07:15

I have been trying to used the following approach in my ASP.NET MVC project where Microsoft.AspNet.SignalR library is used:

public interface ITy         


        
相关标签:
1条回答
  • 2021-01-17 07:45

    Microsoft.AspNetCore.SignalR contains IHubContext for untyped hub

    public interface IHubContext<THub> where THub : Hub
    {
        IHubClients Clients { get; }
        IGroupManager Groups { get; }
    }
    

    and for typed hub

    public interface IHubContext<THub, T> where THub : Hub<T> where T : class
    {
        IHubClients<T> Clients { get; }
        IGroupManager Groups { get; }
    }
    

    As you can see from declarations the THub parameter isn't used anywhere and in fact it exists for dependency injection purposes only.

    Microsoft.AspNet.SignalR in it's turn contains the following IHubContext declarations

    // for untyped hub
    public interface IHubContext
    {
        IHubConnectionContext<dynamic> Clients { get; }
        IGroupManager Groups { get; }
    }
    
    // for typed hub
    public interface IHubContext<T>
    {
        IHubConnectionContext<T> Clients { get; }
        IGroupManager Groups { get; }
    }
    

    As you can see in this case the interfaces don't contain THub parameter and it's not needed because ASP.NET MVC doesn't have built in DI for SignalR. For using typed client it's sufficient to use IHubContext<T> in your case. To use DI you have to "manually inject" hub context as I described it here.

    0 讨论(0)
提交回复
热议问题