通过询问,我已经知道我可以在普通的旧 Windows 服务中使用 SignalR。我想在作为 SignalR 客户端的 Windows 服务和 ASP.NET MVC SignalR 服务器之间进行通信。我用过this hubs guide ,它说
指定 URL 的服务器代码
app.MapSignalR("/signalr", new HubConfiguration());
如果我这样做,消息会去哪里?当你做新的HubConfiguration时,集线器是什么?
它说我可以连接
NET client code that specifies the URL
var hubConnection = new HubConnection("http://contoso.com/signalr", useDefaultUrl: false);
我会将其存储在我的 Windows Server 中,但如果我将文本发送到 http://myserver.com/signalr在服务器上,如何获取?什么枢纽?
另外,最好将其放在 Windows 服务中的哪个位置?举个例子就太好了!
最佳答案
这样就解决了问题
在客户端(Windows 服务):
protected override async void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
try
{
var hubConnection = new HubConnection("http://www.savitassa.com/signalr", useDefaultUrl: false);
IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");
await hubConnection.Start();
await alphaProxy.Invoke("Hello", "Message from Service");
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message);
}
}
在服务器上:
public class AlphaHub : Hub
{
public void Hello(string message)
{
// We got the string from the Windows Service
// using SignalR. Now need to send to the clients
Clients.All.addNewMessageToPage(message);
// Send message to Windows Service
}
关于asp.net-mvc - MVC SignalR 服务器和 Windows 服务 SIignalR 客户端之间的 SignalR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613889/