使用GlobalHost.ConnectionManager.GetHubContext在不同项目的情况下向客户端发送消息不起作用

时间:2015-12-28 15:32:56

标签: signalr-hub

基本上我有两个不同的项目 1)ChatHub和HubCommon是SignalR Hub项目(在IIS中托管) 2)使用GET和POST方法的Web API项目(在IIS中托管)
我正在使用GlobalHost.ConnectionManager.GetHubContext来获取hubcontext而不是将消息广播到所有客户端。我无法向客户端发送消息 请建议从不同项目访问SignalR Hub方法的替代方法。

我尝试的可能选项: - 1)使用GlobalHost.ConnectionManager.GetHubContext [它不工作] 2)实现WebAPI项目作为SignalR .Net客户端[它工作但不想使用这种方法]

请建议任何其他方法来实现这一目标?

Here is my code for reference:-

    //This is SignalR Hub project
        public class ChatHub : Hub
                            {
        //This is my HUB class which sends message using assetid
                                public void Send(string name, string message, string assetid)
                                {
                                    //Send message to specific client based on Asset ID
                                   Clients.Group(assetid).broadcastMessage(name, message);
                                }

                                public override Task OnConnected()
                                {
                                    //Clients.All.reportConnections("A new client connection " + Context.ConnectionId);

                                    //Retrieve the assetid from the query string
                                    var assetid = Context.QueryString["assetid"];

                                    //Single-user groups approach- Create a group for each user using AssetID
                                    Groups.Add(Context.ConnectionId, assetid);
                                    return base.OnConnected();
                                }           
                        }


                         public class HubCommon
                            {
                                           // This class belongs to my ChatHub Project but exposed to my WebAPI project
                                public void SendToHub(string name, string message, string assetid)
                                {

                                     IHubContext _hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
         //This ideally broadcast the message to all the clients but it my case it doesn't
_hubContext.Clients.All.broadcastMessage(name,message);
 }
}               
    //This is WEP API Project
            //This is my Web API POST method which sends message to the all clients
    //associated to Hub                   
                  public string Post([FromBody]Item data)
                        {

                            string message= data.Data[0].Value;

                            Instantiate HubCommon and invoke the SendToHub method
                            HubCommon hubcommon = new HubCommon();
                            hubcommon.SendToHub(Item.datatype, message,Item.assetid);

                            //Test response code
                            return data.datatype;
                        }

1 个答案:

答案 0 :(得分:2)

对于多项目方案,请使用以下

var _hubContext = GlobalHost.DependencyResolver.Resolve<IConnectionManager>().GetHubContext<ChatHub>();

  //This broadcasts the message to all the clients 
 _hubContext.Clients.All.broadcastMessage(name,message);