SignalR-从集线器外部通过另一个项目中的集线器进行广播 [英] SignalR - Broadcasting over a Hub in another Project from outside of a Hub
问题描述
我的解决方案中有两个项目:
I have two projects in my solution:
项目1: SignalRChat(MVC)-工作正常
项目2:
DatabaseWatcherService Windows服务-正常工作
Project 1: "SignalRChat" (MVC) - Works fine
Project 2: "DatabaseWatcherService" Windows Service - Works fine
我正尝试拨打我的电话通过我的Windows服务发出的SignalRChat Hub,它似乎无法正常工作。
I'm trying to make a call to my SignalRChat Hub from my Windows Service and it doesn't appear to be working.
这是我从Windows服务中调用我的Hub的地方(https://github.com/SignalR/SignalR/wiki/Hubs#broadcasting -从一个集线器中移出一个集线器):
This is where I call my Hub from my windows service (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);
}
尝试连接时出现以下错误:
I'm getting the following error when attempting to connect:
Message =远程服务器返回错误:(500)内部服务器错误。
Message=The remote server returned an error: (500) Internal Server Error.
我正在尝试通过 var connection = new HubConnection( http:// localhost:2129);
正在运行我的MVC项目的端口2129。
Port 2129 is what my MVC project is running on.
推荐答案
据我所知,当您从Web应用程序内部调用集线器时。
This will only work, as far as I am aware, when you are calling the hub from within the web application.
为了从Web应用程序外部与集线器进行交互,例如从Windows服务中,您需要查看 SignalR客户端中心文档
In order to interact with the hub from outside of the web application, e.g. from a Windows Service, you will need to take a look at the SignalR Client Hubs documentation
-
将以下NuGet程序包添加到项目中:
Microsoft.AspNet.SignalR.Client
在页面顶部添加以下语句:使用Microsoft.AspNet.SignalR.Client ;
Add the following statement to the top of your page: using Microsoft.AspNet.SignalR.Client;
您需要创建与集线器的连接,然后启动连接。
You would need to create a connection to the hub, and then start the connection.
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世界字符串,然后从此处调用 Clients.All.addNewMessageTopage(message)
In your hub you would then need to have a method for AddNewMessageToPage
which accepts the hello world string and from here call Clients.All.addNewMessageTopage(message)
这篇关于SignalR-从集线器外部通过另一个项目中的集线器进行广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!