You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在Generic Host中使用SignalR Core?实现方法及版本适配咨询

Can I use SignalR Core with the Generic Host?

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.SignalR and Microsoft.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 just Microsoft.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 WebHost to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:34:13