.NET 6 Preview 6 现已推出,其中包括对 ASP.NET Core 的许多重大改进。
Posted 洛阳泰山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET 6 Preview 6 现已推出,其中包括对 ASP.NET Core 的许多重大改进。相关的知识,希望对你有一定的参考价值。
.NET 6 Preview 6 现已推出,其中包括对 ASP.NET Core 的许多重大改进。
以下是此预览版中的新功能:
- 改进的 Blazor 可访问性
- 必需的 Blazor 组件参数
- 用于 javascript 互操作的高效字节数组传输
- 视图组件标签助手的可选参数
- Angular 模板更新为 Angular 12
- 对最少 API 的 OpenAPI 支持
- 将服务注入到没有属性的最小 API 中
[FromServices]
- 为 Kestrel 配置接受套接字
IHttpActivityFeature
- SignalR 连接的长时间运行活动标记
- WebSocket 压缩
- SignalR WebSockets 测试服务器支持
OnCheckSlidingExpiration
控制 cookie 更新的新事件- ClientCertificateMode.DelayCertificate
开始
要在 .NET 6 Preview 6 中开始使用 ASP.NET Core,请安装 .NET 6 SDK。
如果你在 Windows 上使用 Visual Studio,我们建议安装 Visual Studio 2022 的最新预览版。如果您使用的是 macOS,我们建议您安装 Visual Studio 2019 for Mac 8.10 的最新预览版。
要使用 .NET MAUI 和 Blazor 设置跨平台本机应用程序,请参阅.NET MAUI 入门指南 中的最新说明。请务必查看宣布 .NET MAUI 预览版 6 的博客文章,了解有关此版本中 .NET MAUI 新增功能的所有详细信息。
要安装用于提前 (AOT) 编译和运行时重新链接的最新 .NET WebAssembly 工具,请从提升的命令提示符运行以下命令:
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">dotnet workload install microsoft</span></span><span style="color:#000000"><span style="color:#000000">-</span></span><span style="color:#000000"><span style="color:#000000">net</span></span><span style="color:#000000"><span style="color:#000000">-</span></span><span style="color:#000000"><span style="color:#000000">sdk</span></span><span style="color:#000000"><span style="color:#000000">-</span></span><span style="color:#000000"><span style="color:#000000">blazorwebassembly</span></span><span style="color:#000000"><span style="color:#000000">-</span></span><span style="color:#000000"><span style="color:#000000">aot</span></span></code></span></span>
如果您之前安装了 .NET WebAssembly 工具工作负载,您可以通过从提升的命令提示符运行以下命令将其更新到 .NET 6 Preview 5:
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">dotnet workload update</span></span></code></span></span>
升级现有项目
要将现有的 ASP.NET Core 应用程序从 .NET 6 Preview 5 升级到 .NET 6 Preview 6:
- 将所有 Microsoft.AspNetCore.* 包引用更新为.
6.0.0-preview.6.*
- 将所有 Microsoft.Extensions.* 包引用更新为.
6.0.0-preview.6.*
要将 .NET MAUI Blazor 应用从 .NET 6 Preview 5 升级到 .NET 6 Preview 6,我们建议从使用 .NET 6 Preview 5 SDK 创建的新 .NET MAUI Blazor 项目开始,然后从原始项目复制代码。
查看.NET 6 的 ASP.NET Core重大更改的完整列表。
改进的 Blazor 可访问性
我们对默认 Blazor 模板进行了许多更改,以提高使用屏幕阅读器时的可访问性:
- 我们为导航到页面时应宣布的消息添加了属性,并从调查提示组件中删除以淡化其内容。
role="alert"
role="alert"
- 我们更新了
Counter
要添加的组件,以便在计数器更新时读取当前计数。role="status"
- 我们转而在适当的地方使用更多语义标记元素,例如
main
和article
。 - 我们
ul
将NavBar
组件中的替换为 anav
以便其语义更容易识别,因此可以使用常见的屏幕阅读器键盘快捷键直接跳转到它。 - 我们
title
在NavBar
切换按钮上添加了一个,以便清楚地宣布它的用途。
我们还FocusOnNavigate
向 Blazor添加了一个新组件,该组件在从一个页面导航到另一个页面后将 UI 焦点设置为基于 CSS 选择器的元素。您可以在默认 Blazor 模板FocusOnNavigate
中的App
组件中看到正在工作的组件:
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000088"><span style="color:#000088"><Router</span></span> <span style="color:#660066"><span style="color:#660066">AppAssembly</span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#008800"><span style="color:#008800">"@typeof(Program).Assembly"</span></span><span style="color:#000088"><span style="color:#000088">></span></span>
<span style="color:#000088"><span style="color:#000088"><Found</span></span> <span style="color:#660066"><span style="color:#660066">Context</span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#008800"><span style="color:#008800">"routeData"</span></span><span style="color:#000088"><span style="color:#000088">></span></span>
<span style="color:#000088"><span style="color:#000088"><RouteView</span></span> <span style="color:#660066"><span style="color:#660066">RouteData</span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#008800"><span style="color:#008800">"@routeData"</span></span> <span style="color:#660066"><span style="color:#660066">DefaultLayout</span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#008800"><span style="color:#008800">"@typeof(MainLayout)"</span></span> <span style="color:#000088"><span style="color:#000088">/></span></span>
<span style="color:#000088"><span style="color:#000088"><FocusOnNavigate</span></span> <span style="color:#660066"><span style="color:#660066">RouteData</span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#008800"><span style="color:#008800">"@routeData"</span></span> <span style="color:#660066"><span style="color:#660066">Selector</span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#008800"><span style="color:#008800">"h1"</span></span> <span style="color:#000088"><span style="color:#000088">/></span></span>
<span style="color:#000088"><span style="color:#000088"></Found></span></span>
<span style="color:#000088"><span style="color:#000088"><NotFound></span></span>
<span style="color:#000088"><span style="color:#000088"><LayoutView</span></span> <span style="color:#660066"><span style="color:#660066">Layout</span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#008800"><span style="color:#008800">"@typeof(MainLayout)"</span></span><span style="color:#000088"><span style="color:#000088">></span></span>
<span style="color:#000088"><span style="color:#000088"><p</span></span> <span style="color:#660066"><span style="color:#660066">role</span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#008800"><span style="color:#008800">"alert"</span></span><span style="color:#000088"><span style="color:#000088">></span></span><span style="color:#000000"><span style="color:#000000">Sorry, there's nothing at this address.</span></span><span style="color:#000088"><span style="color:#000088"></p></span></span>
<span style="color:#000088"><span style="color:#000088"></LayoutView></span></span>
<span style="color:#000088"><span style="color:#000088"></NotFound></span></span>
<span style="color:#000088"><span style="color:#000088"></Router></span></span></code></span></span>
当Router
导航到一个新页面时,该FocusOnNavigate
组件将焦点设置在该页面的顶级标题上,由屏幕阅读器读取。这是单页应用程序中的一种常见策略,用于确保在使用屏幕阅读器时适当地宣布页面导航。
我们一直在寻找方法来提高使用 ASP.NET Core 和 Blazor 构建的应用程序的可访问性。如果您对如何进一步提高可访问性有想法,可以在GitHub 上与我们分享您的建议。您还可以通过查看最近的ASP.NET Community Standup on Accessibility for Web Developers或观看最近的Let's Learn .NET:Accessibility活动,了解有关使用 .NET 构建可访问应用程序的更多信息。
必需的 Blazor 组件参数
要要求在使用组件时指定 Blazor 组件参数,请使用新属性:[EditorRequired]
SurveyPrompt.razor
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">[</span></span><span style="color:#660066"><span style="color:#660066">EditorRequired</span></span><span style="color:#000000"><span style="color:#000000">]</span></span>
<span style="color:#000000"><span style="color:#000000">[</span></span><span style="color:#660066"><span style="color:#660066">Parameter</span></span><span style="color:#000000"><span style="color:#000000">]</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">public</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">string</span></span> <span style="color:#660066"><span style="color:#660066">Title</span></span> <span style="color:#000000"><span style="color:#000000">{</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">get</span></span><span style="color:#000000"><span style="color:#000000">;</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">set</span></span><span style="color:#000000"><span style="color:#000000">;</span></span> <span style="color:#000000"><span style="color:#000000">}</span></span></code></span></span>
如果用户在未指定所需参数的情况下尝试使用该组件,则会收到警告:
该属性在设计时并作为构建的一部分强制执行。它不是在运行时强制执行的,也不能保证参数值不能为空。[EditorRequired]
JavaScript 互操作中的高效字节数组传输
Blazor 现在在执行 JavaScript 互操作时对字节数组提供更有效的支持。以前,发送到 JavaScript 和从 JavaScript 发送的字节数组是 Base64 编码的,因此它们可以序列化为 JSON,这增加了传输大小和 CPU 负载。这种编码现在已经在 .NET 6 中优化掉了。新的字节数组优化在Uint8Array
从 JavaScript传递到 .NET时对用户是透明的。
将.NET 中的a 传递给 JavaScript 时,字节现在作为 a而不是 Base64 编码字符串接收。需要删除先前解码 Base64 编码字符串的代码。有关此重大更改的详细信息,请参阅相关公告。byte[]
Uint8Array
视图组件标签助手的可选参数
视图组件标签助手现在支持可选参数。如果您的视图组件有一个可选参数,您不再需要为该参数指定一个值作为标签助手属性。
因此,如果您有一个带有可选参数的视图组件,如下所示:
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#1e1eff"><span style="color:#1e1eff">class</span></span> <span style="color:#660066"><span style="color:#660066">MyViewComponent</span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span>
<span style="color:#660066"><span style="color:#660066">IViewComponentResult</span></span> <span style="color:#660066"><span style="color:#660066">Invoke</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#1e1eff"><span style="color:#1e1eff">bool</span></span><span style="color:#000000"><span style="color:#000000"> showSomething </span></span><span style="color:#000000"><span style="color:#000000">=</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">false</span></span><span style="color:#000000"><span style="color:#000000">)</span></span> <span style="color:#000000"><span style="color:#000000">{</span></span> <span style="color:#000000"><span style="color:#000000">...</span></span> <span style="color:#000000"><span style="color:#000000">}</span></span>
<span style="color:#000000"><span style="color:#000000">}</span></span></code></span></span>
那么您现在可以将它作为标签助手调用,而无需为showSomething
参数指定值:
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000088"><span style="color:#000088"><vc:my</span></span> <span style="color:#000088"><span style="color:#000088">/></span></span></code></span></span>
Angular 模板更新为 Angular 12
Angular 的 ASP.NET Core 模板现在使用 Angular 12。
最小 API 中的 OpenAPI 支持
在.NET 6 预览版 4 中,我们宣布了用于在 Web 应用程序中托管和路由的最小 API。我们分享了如何仅用几行代码在单个文件中开发 API。我们新的简化 API 以更少的仪式提供了 ASP.NET 的好处。
今天,我们很高兴最小的 API 现在支持OpenAPI。借助 OpenAPI (Swagger) 支持,您现在可以轻松设置Swagger UI以可视化最少的 API 并与之交互。
使用最少的 API 配置 Swagger UI
要使用最少的 API 尝试 OpenAPI,请使用 .NET CLI 创建一个新的 ASP.NET Core 空 Web 应用程序或在 Visual Studio 中选择“ASP.NET Core Empty”。dotnet new web -o MyApi
将 Swagger UI 添加到您的应用程序
将包添加到您的应用程序Swashbuckle.AspNetCore
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">dotnet </span></span><span style="color:#1e1eff"><span style="color:#1e1eff">add</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">package</span></span> <span style="color:#660066"><span style="color:#660066">Swashbuckle</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">AspNetCore</span></span></code></span></span>
打开并更新以下配置以在您的应用程序中设置 Swagger;您需要更新以下内容:Program.cs
依赖注入:将 Swagger 文档生成器添加AddSwaggerGen
到.builder.services
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">builder</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">Services</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">AddEndpointsApiExplorer</span></span><span style="color:#000000"><span style="color:#000000">();</span></span><span style="color:#000000"><span style="color:#000000">
builder</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">Services</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">AddSwaggerGen</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">c </span></span><span style="color:#000000"><span style="color:#000000">=></span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span><span style="color:#000000"><span style="color:#000000">
c</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">SwaggerDoc</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#980606"><span style="color:#980606">"v1"</span></span><span style="color:#000000"><span style="color:#000000">,</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">new</span></span> <span style="color:#660066"><span style="color:#660066">OpenApiInfo</span></span> <span style="color:#000000"><span style="color:#000000">{</span></span> <span style="color:#660066"><span style="color:#660066">Title</span></span> <span style="color:#000000"><span style="color:#000000">=</span></span> <span style="color:#980606"><span style="color:#980606">"My API"</span></span><span style="color:#000000"><span style="color:#000000">,</span></span> <span style="color:#660066"><span style="color:#660066">Description</span></span> <span style="color:#000000"><span style="color:#000000">=</span></span> <span style="color:#980606"><span style="color:#980606">"Docs for my API"</span></span><span style="color:#000000"><span style="color:#000000">,</span></span> <span style="color:#660066"><span style="color:#660066">Version</span></span> <span style="color:#000000"><span style="color:#000000">=</span></span> <span style="color:#980606"><span style="color:#980606">"v1"</span></span> <span style="color:#000000"><span style="color:#000000">});</span></span>
<span style="color:#000000"><span style="color:#000000">});</span></span></code></span></span>
中间件:调用并将Swagger 文档和 UI 中间件添加到您的应用程序。app.UseSwagger
app.UseSwaggerUI
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">app</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">UseSwagger</span></span><span style="color:#000000"><span style="color:#000000">();</span></span><span style="color:#000000"><span style="color:#000000">
app</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">UseSwaggerUI</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">c </span></span><span style="color:#000000"><span style="color:#000000">=></span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span><span style="color:#000000"><span style="color:#000000">
c</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">SwaggerEndpoint</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#980606"><span style="color:#980606">"/swagger/v1/swagger.json"</span></span><span style="color:#000000"><span style="color:#000000">,</span></span> <span style="color:#980606"><span style="color:#980606">"My API V1"</span></span><span style="color:#000000"><span style="color:#000000">);</span></span>
<span style="color:#000000"><span style="color:#000000">});</span></span></code></span></span>
运行您的应用程序并导航到此 URL https://localhost:5001/swagger。
现在 Swagger UI 已设置,您可以可视化您的 API 并与之交互。
下面的屏幕截图是一个功能更齐全的 API,它与 SQLite 数据库一起运行。我们将在 .NET 6 Preview 7 中看到更多这些特性。
将服务注入到没有属性的最小 API 中[FromServices]
此预览版还允许开发人员在不需要属性的情况下将服务注入到他们的路由处理程序中。[FromServices]
之前:带有属性的代码[FromServices]
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">app</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">MapGet</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#980606"><span style="color:#980606">"/todos"</span></span><span style="color:#000000"><span style="color:#000000">,</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">async</span></span> <span style="color:#000000"><span style="color:#000000">([</span></span><span style="color:#660066"><span style="color:#660066">FromServices</span></span><span style="color:#000000"><span style="color:#000000">]</span></span> <span style="color:#660066"><span style="color:#660066">TodoDbContext</span></span><span style="color:#000000"><span style="color:#000000"> db</span></span><span style="color:#000000"><span style="color:#000000">)</span></span> <span style="color:#000000"><span style="color:#000000">=></span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">return</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">await</span></span><span style="color:#000000"><span style="color:#000000"> db</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">Todos</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">ToListAsync</span></span><span style="color:#000000"><span style="color:#000000">();</span></span>
<span style="color:#000000"><span style="color:#000000">});</span></span></code></span></span>
之后:没有属性的代码[FromServices]
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">app</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">MapGet</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#980606"><span style="color:#980606">"/todos"</span></span><span style="color:#000000"><span style="color:#000000">,</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">async</span></span> <span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#660066"><span style="color:#660066">TodoDbContext</span></span><span style="color:#000000"><span style="color:#000000"> db</span></span><span style="color:#000000"><span style="color:#000000">)</span></span> <span style="color:#000000"><span style="color:#000000">=></span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">return</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">await</span></span><span style="color:#000000"><span style="color:#000000"> db</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">Todos</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">ToListAsync</span></span><span style="color:#000000"><span style="color:#000000">();</span></span>
<span style="color:#000000"><span style="color:#000000">});</span></span></code></span></span>
我们为依赖注入容器添加了一项新功能,称为IServiceProviderIsService
. 此功能允许开发人员查询容器以确定类型是否可解析。在新的路由的API使用此功能,以确定是否一个参数可以从服务的约束。
下面展示了如何使用该代码IServiceProviderIsService
的能力来检测,如果MyService
和NotAService
被依赖注入容器考虑服务:
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#1e1eff"><span style="color:#1e1eff">var</span></span><span style="color:#000000"><span style="color:#000000"> serviceProvider </span></span><span style="color:#000000"><span style="color:#000000">=</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">new</span></span> <span style="color:#660066"><span style="color:#660066">ServiceCollection</span></span><span style="color:#000000"><span style="color:#000000">()</span></span>
<span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">AddSingleton</span></span><span style="color:#000000"><span style="color:#000000"><</span></span><span style="color:#660066"><span style="color:#660066">MyService</span></span><span style="color:#000000"><span style="color:#000000">>()</span></span>
<span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">BuildServiceProvider</span></span><span style="color:#000000"><span style="color:#000000">();</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">var</span></span><span style="color:#000000"><span style="color:#000000"> detector </span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#000000"><span style="color:#000000"> serviceProvider</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">GetService</span></span><span style="color:#000000"><span style="color:#000000"><</span></span><span style="color:#660066"><span style="color:#660066">IServiceProviderIsService</span></span><span style="color:#000000"><span style="color:#000000">>();</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">if</span></span> <span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">detector </span></span><span style="color:#1e1eff"><span style="color:#1e1eff">is</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">not</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">null</span></span><span style="color:#000000"><span style="color:#000000">)</span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span>
<span style="color:#660066"><span style="color:#660066">Console</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">WriteLine</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">$</span></span><span style="color:#980606"><span style="color:#980606">"MyService is a service = {detector.IsService(typeof(MyService))}"</span></span><span style="color:#000000"><span style="color:#000000">);</span></span>
<span style="color:#660066"><span style="color:#660066">Console</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">WriteLine</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">$</span></span><span style="color:#980606"><span style="color:#980606">"NotAService is a service = {detector.IsService(typeof(NotAService))}"</span></span><span style="color:#000000"><span style="color:#000000">);</span></span>
<span style="color:#000000"><span style="color:#000000">}</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">class</span></span> <span style="color:#660066"><span style="color:#660066">MyService</span></span> <span style="color:#000000"><span style="color:#000000">{</span></span> <span style="color:#000000"><span style="color:#000000">}</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">class</span></span> <span style="color:#660066"><span style="color:#660066">NotAService</span></span> <span style="color:#000000"><span style="color:#000000">{</span></span> <span style="color:#000000"><span style="color:#000000">}</span></span></code></span></span>
为 Kestrel 配置接受套接字
我们添加了一个回调SocketTransportOptions
,允许您通过创建自己的套接字并绑定到指定的端点来控制接受套接字的创建。如果您只想在绑定后改变套接字,您可以在 上调用CreateDefaultBoundListenSocket
静态辅助方法SocketTransportOptions
。
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#1e1eff"><span style="color:#1e1eff">var</span></span><span style="color:#000000"><span style="color:#000000"> builder </span></span><span style="color:#000000"><span style="color:#000000">=</span></span> <span style="color:#660066"><span style="color:#660066">WebApplication</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">CreateBuilder</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">args</span></span><span style="color:#000000"><span style="color:#000000">);</span></span><span style="color:#000000"><span style="color:#000000">
builder</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">WebHost</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">UseSockets</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">options </span></span><span style="color:#000000"><span style="color:#000000">=></span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span><span style="color:#000000"><span style="color:#000000">
options</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">CreateBoundListenSocket</span></span> <span style="color:#000000"><span style="color:#000000">=</span></span> <span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">endpoint</span></span><span style="color:#000000"><span style="color:#000000">)</span></span> <span style="color:#000000"><span style="color:#000000">=></span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">var</span></span><span style="color:#000000"><span style="color:#000000"> socket </span></span><span style="color:#000000"><span style="color:#000000">=</span></span> <span style="color:#660066"><span style="color:#660066">SocketTransportOptions</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">CreateDefaultBoundListenSocket</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">endpoint</span></span><span style="color:#000000"><span style="color:#000000">);</span></span>
<span style="color:#008800"><span style="color:#008800">// Modify listen socket</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">return</span></span><span style="color:#000000"><span style="color:#000000"> socket</span></span><span style="color:#000000"><span style="color:#000000">;</span></span>
<span style="color:#000000"><span style="color:#000000">};</span></span>
<span style="color:#000000"><span style="color:#000000">});</span></span></code></span></span>
IHttpActivityFeature
Activity
当存在诊断侦听器时,托管会创建每个请求。现在有一项功能允许中间件访问此活动,因此可以在请求处理期间包含其他信息。
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">app</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">Run</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">context </span></span><span style="color:#000000"><span style="color:#000000">=></span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">var</span></span><span style="color:#000000"><span style="color:#000000"> activity </span></span><span style="color:#000000"><span style="color:#000000">=</span></span><span style="color:#000000"><span style="color:#000000"> context</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">Features</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">Get</span></span><span style="color:#000000"><span style="color:#000000"><</span></span><span style="color:#660066"><span style="color:#660066">IHttpActivityFeature</span></span><span style="color:#000000"><span style="color:#000000">>()?.</span></span><span style="color:#660066"><span style="color:#660066">Activity</span></span><span style="color:#000000"><span style="color:#000000">;</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">if</span></span> <span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">activity </span></span><span style="color:#1e1eff"><span style="color:#1e1eff">is</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">not</span></span> <span style="color:#1e1eff"><span style="color:#1e1eff">null</span></span><span style="color:#000000"><span style="color:#000000">)</span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span><span style="color:#000000"><span style="color:#000000">
activity</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">AddTag</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#980606"><span style="color:#980606">"some_info"</span></span><span style="color:#000000"><span style="color:#000000">,</span></span> <span style="color:#980606"><span style="color:#980606">"true"</span></span><span style="color:#000000"><span style="color:#000000">);</span></span>
<span style="color:#000000"><span style="color:#000000">}</span></span>
<span style="color:#000000"><span style="color:#000000">});</span></span></code></span></span>
SignalR 连接的长时间运行活动标记
SignalR 使用 newIHttpActivityFeature
向请求活动添加“http.long_running”标签。这将被 Azure Monitor Application Insights 等 APM 服务用来过滤 SignalR 请求,以免创建长时间运行的请求警报。
WebSocket 压缩
您现在可以选择接受使用压缩的 WebSocket 连接。默认情况下压缩是关闭的,因为在加密连接上启用压缩会使应用程序受到 CRIME/BREACH 攻击。仅当您知道未发送敏感信息或关闭包含敏感信息且带有重载标志的消息的压缩时,才应启用它。注意:客户端在发送敏感信息时还需要禁用压缩,这在浏览器中使用 WebSockets 时不是一个选项。System.Net.WebSockets.WebSocketMessageFlags.DisableCompression
SendAsync
<span style="color:#212529"><span style="background-color:#f0f0f0 !important"><code><span style="color:#000000"><span style="color:#000000">app</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">Run</span></span><span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">context </span></span><span style="color:#000000"><span style="color:#000000">=></span></span>
<span style="color:#000000"><span style="color:#000000">{</span></span>
<span style="color:#1e1eff"><span style="color:#1e1eff">if</span></span> <span style="color:#000000"><span style="color:#000000">(</span></span><span style="color:#000000"><span style="color:#000000">context</span></span><span style="color:#000000"><span style="color:#000000">.</span></span><span style="color:#660066"><span style="color:#660066">WebSockets</span></span><span style="color:#000000">&以上是关于.NET 6 Preview 6 现已推出,其中包括对 ASP.NET Core 的许多重大改进。的主要内容,如果未能解决你的问题,请参考以下文章
.NET 6 Preview 5 中的 ASP.NET Core 更新
.NET 6 Preview 4 已发布,ASP.NET Core 更新内容