我的解决方案中有两个项目:
Project 1: "SignalRChat" (MVC) - Works fine
Project 2: "DatabaseWatcherService" Windows Service - Works fine
我正在尝试从我的 Windows 服务调用我的 SignalRChat Hub,但它似乎无法正常工作。
这是我从 Windows 服务 ( https://github.com/SignalR/SignalR/wiki/Hubs#broadcasting-over-a-hub-from-outside-of-a-hub ) 调用集线器的地方:
void PerformTimerOperation(object sender, EventArgs e)
{
eventLog1.WriteEntry("Timer ticked...");
var message = "test";
var context = GlobalHost.ConnectionManager.GetHubContext<SignalRChat.ChatHub>();
context.Clients.All.addNewMessageToPage(message);
}
尝试连接时出现以下错误:
Message=The remote server returned an error: (500) Internal Server Error.
我正在尝试通过 var connection = new HubConnection("http://localhost:2129");
2129 端口是我的 MVC 项目运行的地方。
最佳答案
据我所知,这仅在您从 Web 应用程序中调用集线器时有效。
为了从网络应用程序外部与中心交互,例如从 Windows 服务,您需要查看 SignalR Client Hubs documentation
将以下 NuGet 包添加到您的项目中:
Microsoft.AspNet.SignalR.Client
将以下语句添加到页面顶部:
using Microsoft.AspNet.SignalR.Client;
您需要创建到集线器的连接,然后启动连接。
var connection = new HubConnection("http://mysite/");
IHubProxy myHub = connection.CreateHubProxy("MyHub");
connection.Start().Wait(); // not sure if you need this if you are simply posting to the hub
myHub.Invoke("addNewMessageToPage", "Hello World");
然后在您的集线器中,您将需要一个用于 AddNewMessageToPage
的方法,该方法接受 hello world 字符串并从此处调用 Clients.All.addNewMessageTopage(message)
关于c# - SignalR - 从集线器外部通过另一个项目中的集线器广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170373/