How to use SignalR hub instance outside of the hubpipleline

前端 未结 2 1906
天命终不由人
天命终不由人 2020-12-02 11:49

I am using SignalR to broadcast messages to all my clients. I need to trigger the broadcasting outside of my hub class i.e. something like below:

var broadcast

2条回答
  •  半阙折子戏
    2020-12-02 12:31

    A small update for those who might be wondering where the GlobalHost has gone. SignalR has been completely rewritten for .net core. So if you are using the SignalR.Core package (Difference between SignalR versions), you get an instance of SignalR hub context by injecting it into your service:

    public class MyNeedyService
    {
        private readonly IHubContext ctx;
    
        public MyNeedyService(IHubContext ctx)
        {
            this.ctx = ctx;
        }
    
        public async Task MyMethod()
        {
            await this.ctx.All.SendAsync("clientCall");
        }
    }
    

    And in Startup.cs:

    services.AddSignalR()/*.AddAzureSignalR("...")*/;
    

    Microsoft docu is here: Send SignalR messages from outside the hub.

提交回复
热议问题