Search code examples
c#asp.netsignalrasp.net-core-signalr

How to connect to SignalR server from ASP.NET Core server and keep it open?


I have an ASP.NET Core back-end web server which does not run a SignalR server/hub. However, the back-end needs to connect to a SignalR hub as a client and keep that connection open all the time to keep some data in sync. How can I do that? I'm not sure IHostedService fits my purpose.


Solution

  • IHostedService is exactly intended for scenarios like these - when you have something running in background for the entire lifetime of your app (i.e. while the executable is running).

    You can use System.Net.WebSockets.ClientWebSocket to connect to the server from your application