How to set up SignalR in your ASP.NET Core 3.1 project.
Install the Microsoft.AspNetCore.SignalR NuGet package:
Create a Hubs folder in your project, and create MySignalRHub.cs inside of it:
Update MySignalRHub.cs like so:
Use dependency injection to access your DbContext (or whatever you use for accessing your database). In the following image, the UserManager is also injected so that we can determine the user:
Now add the JavaScript file required for using SignalR on your webpages. The Microsoft docs show how to do this with npm, a CDN, and LibMan. I use the LibMan method to download signalr.js (and signalr.min.js):
Add a reference to the JS file (I’m putting it in _Layout.cshtml so that it gets loaded for every page):
Update Startup.cs to add SignalR services and map a URL to your Hub:
Now for the client-side code (I’m just sticking this in Home/Index.cshtml for this example):
This image shows how things are connected:
Fredia Raynor Manolo says
Völlig komponiertes Themenmaterial, danke für die selektive Information. Fredia Raynor Manolo
Josh Withee says
Ich bin froh, dass es hilfreich ist!