ASP.NET Core 2.2 的新增功能What's new in ASP.NET Core 2.2

本文重点介绍 ASP.NET Core 2.2 中最重要的更改,并提供相关文档的链接。This article highlights the most significant changes in ASP.NET Core 2.2, with links to relevant documentation.

开放 API 分析器和约定OpenAPI Analyzers & Conventions

开放 API(以前称为 Swagger)是一个与语言无关的规范,用于描述 REST API。OpenAPI (formerly known as Swagger) is a language-agnostic specification for describing REST APIs. 开放 API 生态系统具有一些工具,可用于发现、测试和生成使用该规范的客户端代码。The OpenAPI ecosystem has tools that allow for discovering, testing, and producing client code using the specification. 对在 ASP.NET Core MVC 中生成和直观呈现 OpenAPI 文档的支持是通过社区驱动项目(如 NSwagSwashbuckle.AspNetCore)提供。Support for generating and visualizing OpenAPI documents in ASP.NET Core MVC is provided via community driven projects such as NSwag and Swashbuckle.AspNetCore. 对于创建开放 API 文档,ASP.NET Core 2.2 提供了改进的工具和运行时体验。ASP.NET Core 2.2 provides improved tooling and runtime experiences for creating OpenAPI documents.

有关更多信息,请参见以下资源:For more information, see the following resources:

问题详细信息支持Problem details support

ASP.NET Core 2.1 引入了 ProblemDetails(基于用于通过 HTTP 响应传送错误详细信息的 RFC 7807 规范)。ASP.NET Core 2.1 introduced ProblemDetails, based on the RFC 7807 specification for carrying details of an error with an HTTP Response. 在 2.2 中,ProblemDetails 是针对具有 ApiControllerAttribute 特性的控制器中客户端错误代码的标准响应。In 2.2, ProblemDetails is the standard response for client error codes in controllers attributed with ApiControllerAttribute. 返回客户端错误状态代码 (4xx) 的 IActionResult 现在返回 ProblemDetails 正文。An IActionResult returning a client error status code (4xx) now returns a ProblemDetails body. 结果还包括可以用于将使用请求日志的错误相关联的关联 ID。The result also includes a correlation ID that can be used to correlate the error using request logs. 对于客户端错误,ProducesResponseType 默认为使用 ProblemDetails 作为响应类型。For client errors, ProducesResponseType defaults to using ProblemDetails as the response type. 这会记录在使用 NSwag 或 Swashbuckle.AspNetCore 生成的开放 API/Swagger 输出中。This is documented in OpenAPI / Swagger output generated using NSwag or Swashbuckle.AspNetCore.

终结点路由Endpoint Routing

ASP.NET Core 2.2 使用新的终结点路由 系统来改进请求的调度。ASP.NET Core 2.2 uses a new endpoint routing system for improved dispatching of requests. 更改包括新链接生成 API 成员和路由参数转换器。The changes include new link generation API members and route parameter transformers.

有关更多信息,请参见以下资源:For more information, see the following resources:

运行状况检查Health checks

通过新的运行状况检查服务可以更轻松地在需要运行状况检查的环境(如 Kubernetes)中使用 ASP.NET Core。A new health checks service makes it easier to use ASP.NET Core in environments that require health checks, such as Kubernetes. 运行状况检查包括中间件和一组定义 IHealthCheck 抽象和服务的库。Health checks includes middleware and a set of libraries that define an IHealthCheck abstraction and service.

运行状况检查由容器业务流程协调程序或负载均衡器用于快速确定系统是否正常响应请求。Health checks are used by a container orchestrator or load balancer to quickly determine if a system is responding to requests normally. 容器业务流程协调程序可以通过停止滚动部署或重新启动容器来响应失败的运行状况检查。A container orchestrator might respond to a failing health check by halting a rolling deployment or restarting a container. 负载均衡器可以通过路由流量以避开失败的服务实例,来响应运行状况检查。A load balancer might respond to a health check by routing traffic away from the failing instance of the service.

运行状况检查由应用程序公开为监视系统所使用的 HTTP 终结点。Health checks are exposed by an application as an HTTP endpoint used by monitoring systems. 可以为各种实时监视方案和监视系统配置运行状况检查。Health checks can be configured for a variety of real-time monitoring scenarios and monitoring systems. 运行状况检查服务与 BeatPulse 项目集成。The health checks service integrates with the BeatPulse project. 从而可以更轻松地添加为众多常用系统和依赖项添加检查。which makes it easier to add checks for dozens of popular systems and dependencies.

有关详细信息,请参阅 ASP.NET Core 中的运行状况检查For more information, see Health checks in ASP.NET Core.

Kestrel 中的 HTTP/2HTTP/2 in Kestrel

ASP.NET Core 2.2 添加了对 HTTP/2 的支持。ASP.NET Core 2.2 adds support for HTTP/2.

HTTP/2 是 HTTP 协议的主要修订版本。HTTP/2 is a major revision of the HTTP protocol. HTTP/2 的重要功能包括:Notable features of HTTP/2 include:

  • 标头压缩支持。Support for header compression.
  • 单个连接上的完全多路复用流。Fully multiplexed streams over a single connection.

尽管 HTTP/2 保留了 HTTP 的语义(如 HTTP 标头和方法),不过与 HTTP/1.x 相比,在客户端和服务器之间对此数据进行组帧和发送的方式发生了重大更改。While HTTP/2 preserves HTTP's semantics (for example, HTTP headers and methods), it's a breaking change from HTTP/1.x on how data is framed and sent between the client and server.

由于组帧中的这一更改,服务器和客户端需要协商所使用的协议版本。As a consequence of this change in framing, servers and clients need to negotiate the protocol version used. 应用层协议协商 (ALPN) 是一种 TLS 扩展,允许服务器和客户端协商用作其 TLS 握手一部分的协议版本。Application-Layer Protocol Negotiation (ALPN) is a TLS extension that allows the server and client to negotiate the protocol version used as part of their TLS handshake. 虽然服务器与客户端之间可以事先了解协议,不过所有主要浏览器都支持 ALPN 作为建立 HTTP/2 连接的唯一方法。While it is possible to have prior knowledge between the server and the client on the protocol, all major browsers support ALPN as the only way to establish an HTTP/2 connection.

有关详细信息,请参阅 HTTP/2 支持For more information, see HTTP/2 support.

Kestrel 配置Kestrel configuration

在早期版本的 ASP.NET Core 中,Kestrel 选项通过调用 UseKestrel 来配置。In earlier versions of ASP.NET Core, Kestrel options are configured by calling UseKestrel. 在 2.2 中,Kestrel 选项通过在主机生成器上调用 ConfigureKestrel 来配置。In 2.2, Kestrel options are configured by calling ConfigureKestrel on the host builder. 此更改为进程内承载解决了 IServer 注册顺序方面的问题。This change resolves an issue with the order of IServer registrations for in-process hosting. 有关更多信息,请参见以下资源:For more information, see the following resources:

IIS 进程内承载IIS in-process hosting

在早期版本的 ASP.NET Core 中,IIS 用作反向代理。In earlier versions of ASP.NET Core, IIS serves as a reverse proxy. 在 2.2 中,ASP.NET Core 模块可以启动 CoreCLR 并在 IIS 工作进程 (w3wp.exe ) 内承载应用。In 2.2, the ASP.NET Core Module can boot the CoreCLR and host an app inside the IIS worker process (w3wp.exe). 在使用 IIS 运行时,进程内承载可提供性能和诊断提升。In-process hosting provides performance and diagnostic gains when running with IIS.

有关详细信息,请参阅 IIS 进程内承载For more information, see in-process hosting for IIS.

SignalR Java 客户端 Java client

ASP.NET Core 2.2 引入了适用于 SignalR 的 Java 客户端。ASP.NET Core 2.2 introduces a Java Client for SignalR. 此客户端支持通过 Java 代码连接到 ASP.NET Core SignalR Server(包括 Android 应用)。This client supports connecting to an ASP.NET Core SignalR Server from Java code, including Android apps.

有关详细信息,请参阅 ASP.NET Core SignalR Java 客户端For more information, see ASP.NET Core SignalR Java client.

CORS 改进CORS improvements

在早期版本的 ASP.NET Core 中,CORS 中间件允许发送 AcceptAccept-LanguageContent-LanguageOrigin 标头(不考虑在 CorsPolicy.Headers 中配置的值)。In earlier versions of ASP.NET Core, CORS Middleware allows Accept, Accept-Language, Content-Language, and Origin headers to be sent regardless of the values configured in CorsPolicy.Headers. 在 2.2 中,仅当在 Access-Control-Request-Headers 中发送的标头与 WithHeaders 中声明的标头完全匹配时,才能进行 CORS 中间件策略匹配。In 2.2, a CORS Middleware policy match is only possible when the headers sent in Access-Control-Request-Headers exactly match the headers stated in WithHeaders.

有关详细信息,请参阅 CORS 中间件For more information, see CORS Middleware.

响应压缩Response compression

ASP.NET Core 2.2 可以使用 Brotli 压缩格式来压缩响应。ASP.NET Core 2.2 can compress responses with the Brotli compression format.

有关详细信息,请参阅响应压缩中间件支持 Brotli 压缩For more information, see Response Compression Middleware supports Brotli compression.

项目模板Project templates

ASP.NET Core Web 项目模板已更新为 Bootstrap 4Angular 6ASP.NET Core web project templates were updated to Bootstrap 4 and Angular 6. 新的外观在视觉上更简单,可以更轻松地查看应用的重要结构。The new look is visually simpler and makes it easier to see the important structures of the app.

主页或索引页

验证性能Validation performance

根据设计,MVC 的验证系统具有可扩展性和灵活性,让你能够基于每个请求确定应用于给定模型的验证程序。MVC's validation system is designed to be extensible and flexible, allowing you to determine on a per request basis which validators apply to a given model. 这非常适用于创作复杂的验证提供程序。This is great for authoring complex validation providers. 但最常见的情况是,应用程序仅使用内置验证程序,不需要这种额外的灵活性。However, in the most common case an application only uses the built-in validators and don't require this extra flexibility. 内置验证程序包括 DataAnnotations(如 [Required] 和 [StringLength])和 IValidatableObjectBuilt-in validators include DataAnnotations such as [Required] and [StringLength], and IValidatableObject.

在 ASP.NET Core 2.2 中,如果 MVC 确定给定模型关系图不需要验证,则可以绕过验证。In ASP.NET Core 2.2, MVC can short-circuit validation if it determines that a given model graph doesn't require validation. 在验证无法具有或不具有任何验证程序的模型时,跳过验证可获得显著改进。Skipping validation results in significant improvements when validating models that can't or don't have any validators. 这包括诸如基元集合(如 byte[]string[]Dictionary<string, string> )之类的对象,或没有许多验证程序的复杂对象关系图。This includes objects such as collections of primitives (such as byte[], string[], Dictionary<string, string>), or complex object graphs without many validators.

HTTP 客户端性能HTTP Client performance

在 ASP.NET Core 2.2 中,通过减少连接池锁定争用提高了 SocketsHttpHandler 的性能。In ASP.NET Core 2.2, the performance of SocketsHttpHandler was improved by reducing connection pool locking contention. 对于进行大量传出 HTTP 请求的应用(如某些微服务体系结构),吞吐量得到了提高。For apps that make many outgoing HTTP requests, such as some microservices architectures, throughput is improved. 在负载下,HttpClient 吞吐量在 Linux 上可以提高多达 60%,在 Windows 上提高多达 20%。Under load, HttpClient throughput can be improved by up to 60% on Linux and 20% on Windows.

有关详细信息,请参阅实现此改进的拉取请求For more information, see the pull request that made this improvement.

其他信息Additional information

要获取完整的更改列表,请参阅 ASP.NET Core 2.2 发行说明For the complete list of changes, see the ASP.NET Core 2.2 Release Notes.