ASP.NET SignalR 和 ASP.NET Core SignalR 之间的差异Differences between ASP.NET SignalR and ASP.NET Core SignalR

ASP.NET Core SignalR 与 ASP.NET SignalR 的客户端或服务器不兼容。ASP.NET Core SignalR isn't compatible with clients or servers for ASP.NET SignalR. 本文详细介绍 ASP.NET Core SignalR 中已删除或已更改的功能。This article details features which have been removed or changed in ASP.NET Core SignalR.

如何识别 SignalR 版本How to identify the SignalR version

ASP.NET SignalRASP.NET SignalR ASP.NET Core SignalRASP.NET Core SignalR
服务器 NuGet 包Server NuGet Package Microsoft.AspNet.SignalRMicrosoft.AspNet.SignalR Microsoft.AspNetCore.App (.NET Core)Microsoft.AspNetCore.App (.NET Core)
Microsoft.AspNetCore.SignalR (.NET Framework)Microsoft.AspNetCore.SignalR (.NET Framework)
客户端 NuGet 包Client NuGet Packages Microsoft.AspNet.SignalR.ClientMicrosoft.AspNet.SignalR.Client
Microsoft.AspNet.SignalR.JSMicrosoft.AspNet.SignalR.JS
Microsoft.AspNetCore.SignalR.ClientMicrosoft.AspNetCore.SignalR.Client
客户端 npm 包Client npm Package signalrsignalr @aspnet/signalr
Java 客户端Java Client GitHub 存储库弃用GitHub Repository (deprecated) Maven 包signalrMaven package com.microsoft.signalr
服务器应用类型Server App Type ASP.NET (System.Web) 或 OWIN 自承载ASP.NET (System.Web) or OWIN Self-Host ASP.NET CoreASP.NET Core
受支持的服务器平台Supported Server Platforms .NET framework 4.5 或更高版本.NET Framework 4.5 or later .NET Framework 4.6.1 或更高版本.NET Framework 4.6.1 or later
.NET core 2.1 或更高版本.NET Core 2.1 or later

功能差异Feature differences

自动重新连接Automatic reconnects

ASP.NET Core SignalR 不支持自动重新连接。Automatic reconnects aren't supported in ASP.NET Core SignalR. 如果客户端已断开连接,则用户必须显式启动新连接才能重新连接。If the client is disconnected, the user must explicitly start a new connection if they want to reconnect. 在 ASP.NET SignalR 中,如果连接断开,SignalR 会尝试重新连接到服务器。In ASP.NET SignalR, SignalR attempts to reconnect to the server if the connection is dropped.

协议支持Protocol support

ASP.NET Core SignalR 支持 JSON 以及一种基于 MessagePack 的新二进制协议。ASP.NET Core SignalR supports JSON, as well as a new binary protocol based on MessagePack. 此外,还可创建自定义协议。Additionally, custom protocols can be created.

传输Transports

ASP.NET Core SignalR 不支持永久帧传输。The Forever Frame transport is not supported in ASP.NET Core SignalR.

服务器上的差异Differences on the server

ASP.NET Core SignalR 服务器端库包含在 Microsoft.AspNetCore.App 元包中,该包属于适用于 Razor 和 MVC 项目的 ASP.NET Core Web 应用程序模板。The ASP.NET Core SignalR server-side libraries are included in the Microsoft.AspNetCore.App metapackage package that's part of the ASP.NET Core Web Application template for both Razor and MVC projects.

ASP.NET Core SignalR 是一个 ASP.NET Core 中间件,因此必须通过在 Startup.ConfigureServices 中调用 AddSignalR 来配置它。ASP.NET Core SignalR is an ASP.NET Core middleware, so it must be configured by calling AddSignalR in Startup.ConfigureServices.

services.AddSignalR()

若要配置路由,请将路由映射到Startup.Configure 方法中的 UseEndpoints 方法调用内的中心。To configure routing, map routes to hubs inside the UseEndpoints method call in the Startup.Configure method.

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    endpoints.MapHub<ChatHub>("/hub");
});

若要配置路由,请在 Startup.Configure 方法中将路由映射到 UseSignalR 方法调用内的中心。To configure routing, map routes to hubs inside the UseSignalR method call in the Startup.Configure method.

app.UseSignalR(routes =>
{
    routes.MapHub<ChatHub>("/hub");
});

粘滞会话Sticky sessions

ASP.NET SignalR 的横向扩展模型允许客户端重新连接场中的任何服务器并将消息发送到这些服务器。The scaleout model for ASP.NET SignalR allows clients to reconnect and send messages to any server in the farm. 在 ASP.NET Core SignalR 中,客户端必须在连接期间与同一服务器进行交互。In ASP.NET Core SignalR, the client must interact with the same server for the duration of the connection. 对于使用 Redis 的横向扩展,这意味着需要粘滞会话。For scaleout using Redis, that means sticky sessions are required. 对于使用 Azure SignalR 服务的横向扩展,则不需要粘滞会话,因为该服务会处理与客户端的连接。For scaleout using Azure SignalR Service, sticky sessions are not required because the service handles connections to clients.

一个连接一个中心Single hub per connection

在ASP.NET Core SignalR中,连接模型已经简化。In ASP.NET Core SignalR, the connection model has been simplified. 可直接连接到单个中心,不必通过单个连接来共享对多个中心的访问。Connections are made directly to a single hub, rather than a single connection being used to share access to multiple hubs.

流式处理Streaming

ASP.NET Core SignalR 现在支持从中心流式传输数据到客户端。ASP.NET Core SignalR now supports streaming data from the hub to the client.

状态State

已删除在客户端和中心之间传递任意状态(通常称为 HubState)的功能以及对进度消息的支持。The ability to pass arbitrary state between clients and the hub (often called HubState) has been removed, as well as support for progress messages. 目前没有中心代理的对应项。There is no counterpart of hub proxies at the moment.

删除 PersistentConnectionPersistentConnection removal

在 ASP.NET Core SignalR 中,PersistentConnection 类已删除。In ASP.NET Core SignalR, the PersistentConnection class has been removed.

GlobalHostGlobalHost

ASP.NET Core 在框架中内置了依赖关系注入 (DI)。ASP.NET Core has dependency injection (DI) built into the framework. 服务可以使用 DI 来访问 HubContextServices can use DI to access the HubContext. ASP.NET CoreR SignalR 中不存在用于在 ASP.NET SignalR 中获取 HubContextGlobalHost 对象。The GlobalHost object that is used in ASP.NET SignalR to get a HubContext doesn't exist in ASP.NET Core SignalR.

HubPipelineHubPipeline

ASP.NET Core SignalR 没有对 HubPipeline 模块的支持。ASP.NET Core SignalR doesn't have support for HubPipeline modules.

客户端上的差异Differences on the client

TypeScriptTypeScript

ASP.NET Core SignalR 客户端以 TypeScript 编写。The ASP.NET Core SignalR client is written in TypeScript. 使用 JavaScript 客户端时,可以以 JavaScript 或 TypeScript 编写。You can write in JavaScript or TypeScript when using the JavaScript client.

JavaScript 客户端托管在 npmThe JavaScript client is hosted at npm

在以前的版本中,JavaScript 客户端是通过 Visual Studio 中的 NuGet 包获取的。In previous versions, the JavaScript client was obtained through a NuGet package in Visual Studio. Core 版本的 @aspnet/signalr npm 包包含 JavaScript 库。For the Core versions, the @aspnet/signalr npm package contains the JavaScript libraries. 此包不包括在ASP.NET Core Web 应用程序模板。This package isn't included in the ASP.NET Core Web Application template. 使用 npm 获取并安装 @aspnet/signalr npm 包。Use npm to obtain and install the @aspnet/signalr npm package.

npm init -y
npm install @aspnet/signalr

jQueryjQuery

已删除对 jQuery 的依赖关系,但项目仍然可以使用 jQuery。The dependency on jQuery has been removed, however projects can still use jQuery.

Internet Explorer 支持Internet Explorer support

ASP.NET Core SignalR 需要 Microsoft Internet Explorer 11 或更高版本(ASP.NET SignalR 支持 Microsoft Internet Explorer 8 及更高版本)。ASP.NET Core SignalR requires Microsoft Internet Explorer 11 or later (ASP.NET SignalR supported Microsoft Internet Explorer 8 and later).

JavaScript 客户端方法语法JavaScript client method syntax

JavaScript 语法已与 Signalr 早期版本中的相应语法不同。The JavaScript syntax has changed from the previous version of SignalR. 请使用 HubConnectionBuilder API 而非 $connection 对象创建连接。Rather than using the $connection object, create a connection using the HubConnectionBuilder API.

const connection = new signalR.HubConnectionBuilder()
    .withUrl("/hub")
    .build();

使用 on 方法指定可供中心调用的客户端方法。Use the on method to specify client methods that the hub can call.

connection.on("ReceiveMessage", (user, message) => {
    const msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    const encodedMsg = user + " says " + msg;
    log(encodedMsg);
});

创建客户端方法后,启动中心连接。After creating the client method, start the hub connection. 链接一种 catch 方法以记录或处理错误。Chain a catch method to log or handle errors.

connection.start().catch(err => console.error(err.toString()));

中心代理Hub proxies

中心代理不再自动生成。Hub proxies are no longer automatically generated. 与之相反,方法名称将以字符串形式传递到 invoke API 中。Instead, the method name is passed into the invoke API as a string.

.NET 和其他客户端.NET and other clients

Microsoft.AspNetCore.SignalR.Client NuGet 包中包含 ASP.NET Core SignalR 的 .NET 客户端库。The Microsoft.AspNetCore.SignalR.Client NuGet package contains the .NET client libraries for ASP.NET Core SignalR.

使用 HubConnectionBuilder 创建和生成到中心的连接的实例。Use the HubConnectionBuilder to create and build an instance of a connection to a hub.

connection = new HubConnectionBuilder()
    .WithUrl("url")
    .Build();

横向扩展差异Scaleout differences

ASP.NET SignalR 支持 SQL Server 和 Redis。ASP.NET SignalR supports SQL Server and Redis. ASP.NET Core SignalR 支持 Azure SignalR 服务和 Redis。ASP.NET Core SignalR supports Azure SignalR Service and Redis.

ASP.NETASP.NET

ASP.NET CoreASP.NET Core

其他资源Additional resources