能否在Generic Host中使用SignalR Core?实现方法及版本适配咨询
Absolutely! You can absolutely use SignalR Core with the Generic Host in both .NET Core 2.2 and the .NET Core 3.0 preview. The setup is straightforward, and 3.0 even streamlines the process by integrating web hosting directly into the Generic Host. Let me walk you through the implementation step by step.
Step 1: Install Required NuGet Packages
First, make sure you have the necessary packages installed:
- For .NET Core 2.2: Install
Microsoft.AspNetCore.SignalRandMicrosoft.AspNetCore.Hosting(to embed web hosting within the Generic Host) - For .NET Core 3.0 Preview: Install
Microsoft.AspNetCore.App(the metapackage that includes SignalR) or justMicrosoft.AspNetCore.SignalR
Step 2: Configure the Generic Host with SignalR
Let's modify your existing Main method to add SignalR support.
For .NET Core 2.2
You'll need to wrap the web host configuration inside the Generic Host builder:
using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; public static async Task Main(string[] args) { var host = new HostBuilder() // Configure web hosting to support SignalR's HTTP traffic .ConfigureWebHost(webBuilder => { webBuilder.UseKestrel(); webBuilder.ConfigureServices(services => { // Register SignalR core services services.AddSignalR(); }); webBuilder.Configure(app => { // Map your SignalR hub to a specific endpoint app.UseSignalR(routes => { routes.MapHub<ChatHub>("/chatHub"); }); }); }) .Build(); await host.RunAsync(); }
For .NET Core 3.0 Preview
The Generic Host now has native support for web hosting via ConfigureWebHostDefaults, making the code cleaner and more unified:
using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; public static async Task Main(string[] args) { var host = Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.ConfigureServices(services => { // Register SignalR core services services.AddSignalR(); }); webBuilder.Configure(app => { // Use 3.0's new endpoint routing system for SignalR app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapHub<ChatHub>("/chatHub"); }); }); }) .Build(); await host.RunAsync(); }
Step 3: Create Your SignalR Hub
Now create a simple Hub class to handle client-to-server (and server-to-client) interactions:
using Microsoft.AspNetCore.SignalR; using System.Threading.Tasks; public class ChatHub : Hub { // Example method to broadcast messages to all connected clients public async Task SendMessage(string user, string message) { await Clients.All.SendAsync("ReceiveMessage", user, message); } }
Quick Notes
- In .NET Core 2.2, the Generic Host relies on embedding a separate
WebHostto handle the HTTP traffic SignalR requires. - .NET Core 3.0 unifies the Generic Host and WebHost, so you don't need to manually wire up a separate web host instance.
- You can customize Kestrel settings (like ports or HTTPS) in the web builder configuration if needed.
内容的提问来源于stack exchange,提问作者El Mac

