在不同项目的情况下使用 GlobalHost.ConnectionManager.GetHubContext 向客户端发送消息不起作用
Using GlobalHost.ConnectionManager.GetHubContext to send message to client in case of different projects doesn't work
基本上我有两个不同的项目
1)ChatHub 和 HubCommon 是 SignalR Hub 项目(托管在 IIS 中)
2) 使用 GET 和 POST 方法的 Web API 项目(在 IIS 中托管)
我正在使用 GlobalHost.ConnectionManager.GetHubContext 获取 hubcontext,而不是将消息广播给所有 clients.Im 无法向客户端发送消息的人
请建议从不同项目访问 SignalR Hub 方法的替代方法。
我尝试过的可能选项:-
1) 使用 GlobalHost.ConnectionManager.GetHubContext [不起作用]
2) 将 WebAPI 项目作为 SignalR .Net Client 实施 [有效但不想使用这种方法]
请提出任何其他实现此目的的方法??
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;
}
对于多项目场景,使用如下
var _hubContext = GlobalHost.DependencyResolver.Resolve<IConnectionManager>().GetHubContext<ChatHub>();
//This broadcasts the message to all the clients
_hubContext.Clients.All.broadcastMessage(name,message);
基本上我有两个不同的项目
1)ChatHub 和 HubCommon 是 SignalR Hub 项目(托管在 IIS 中)
2) 使用 GET 和 POST 方法的 Web API 项目(在 IIS 中托管)
我正在使用 GlobalHost.ConnectionManager.GetHubContext 获取 hubcontext,而不是将消息广播给所有 clients.Im 无法向客户端发送消息的人
请建议从不同项目访问 SignalR Hub 方法的替代方法。
我尝试过的可能选项:- 1) 使用 GlobalHost.ConnectionManager.GetHubContext [不起作用] 2) 将 WebAPI 项目作为 SignalR .Net Client 实施 [有效但不想使用这种方法]
请提出任何其他实现此目的的方法??
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;
}
对于多项目场景,使用如下
var _hubContext = GlobalHost.DependencyResolver.Resolve<IConnectionManager>().GetHubContext<ChatHub>();
//This broadcasts the message to all the clients
_hubContext.Clients.All.broadcastMessage(name,message);