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

僤鯓⒐⒋嵵緔 提交于 2019-12-06 17:24:59

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