How to broadcast events from Web Api project to MVC app on a different App Pool using SignalR

瘦欲@ 提交于 2019-12-08 03:20:29

问题


My windows server running IIS has an MVC application in one app pool and a Web Api project in another. These projects were developed in different solutions. The web app currently communicates with the Web Api to use as a rest service. I need to add a SignalR hub to the Web Api project so that it can broadcast some data to the web app in certain situations.

How can I go about doing this? I have read in other posts that you need to use Sql server as a backplane but I'm not sure that is necessary since the two apps are on the same server. If this is the case though, how does the Web app pick up the broadcast when the hub code is not in the project?


回答1:


Since you are only using a single server, I would stay away from backbone scale out (i.e. SQL Server message bus, redis etc) until you are forced to scale out to multiple servers, in my experience SignalR scale out can be painful for multiple reasons.

Because you have the solutions in different app pools, you will need to think of one as the "SignalR Server" and the other as a "Client". I would pick the Web API as the server project and the web app as the client.

In order to communicate from the "Server" to the "Client" you should treat the web app as any other .net client (i.e. you can use a console app as a signalR client). At start up, or at some other event in your web app you will have to register with the "Server"(web api) that you are listening out for messages that signalR has to send. There is many great tutorials on this over at Asp.net, at the time of writing this link explains how you could go about setting all this up, I have extreamly simplified this tutorial below:

1). install Microsoft.AspNet.SignalR.Client from Nuget

2). register with the hub and start an async connection:

var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price));
await hubConnection.Start();


来源:https://stackoverflow.com/questions/29932936/how-to-broadcast-events-from-web-api-project-to-mvc-app-on-a-different-app-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!