.NET Aspire Azure Web PubSub 集成
本文介绍如何使用 .NET AspireAzure Web PubSub 集成。
Aspire.Azure.Messaging.WebPubSub
库提供了用于在 DI 容器中注册 WebPubSubServiceClient 以连接到 Azure Web PubSub的选项。
先决条件
- Azure 订阅:免费创建一个订阅。
- 现有 Azure Web PubSub 服务实例。 有关详细信息,请参阅 创建 Web PubSub 资源。 或者,您可以使用连接字符串,但不建议在生产环境中使用。
入门
若要开始使用 .NET AspireAzure Web PubSub 集成,请安装 📦Aspire。Azure。Messaging.WebPubSub 客户端使用项目中的 NuGet 包,即使用 Azure Web PubSub 客户端的应用程序的项目。
dotnet add package Aspire.Azure.Messaging.WebPubSub
有关详细信息,请参阅 dotnet add package 或 在 .NET 应用程序中管理包依赖关系。
示例用法
在项目的 Program.cs 文件中,调用 AddAzureWebPubSubHub
扩展方法来注册 WebPubSubServiceClient
,以便通过依赖项注入容器使用。 该方法采用连接名称参数。
builder.AddAzureWebPubSubServiceClient("wps");
然后,可以使用依赖项注入检索 WebPubSubServiceClient
实例。 例如,若要从服务检索客户端:
public class ExampleService(WebPubSubServiceClient client)
{
// Use client...
}
有关更多信息,请参阅 Azure的 .Messaging.WebPubSub 文档。
应用主机使用情况
若要将 Azure Web PubSub 托管支持添加到 IDistributedApplicationBuilder,请在 📦 项目中安装 AspireAzure.Hosting..WebPubSub NuGet 包。
dotnet add package Aspire.Hosting.Azure.WebPubSub
在应用主机项目中,添加 Web PubSub 连接并使用以下方法使用该连接:
var webPubSub = builder.AddAzureWebPubSub("wps");
var exampleService = builder.AddProject<Projects.ExampleService>()
.WithReference(webPubSub);
AddAzureWebPubSubHub
方法从应用主机的配置中读取连接信息(例如,从 ConnectionStrings:wps
配置密钥下的“用户机密”)。
WithReference
方法将该连接信息传递到 wps
项目中名为 ExampleService
的连接字符串中。 在 的 ExampleService
文件中,可以使用以下方式消耗连接:
builder.AddAzureWebPubSubServiceClient("wps");
配置
.NET Aspire
Azure Web PubSub 库提供了多个选项,用于根据项目的要求和约定配置 Azure Web PubSub 连接。 请注意,必须提供 Endpoint
或 ConnectionString
中的一个。
使用连接字符串
使用 ConnectionStrings
配置部分中的连接字符串时,可以在调用 builder.AddAzureWebPubSubHub()
时提供连接字符串的名称:
builder.AddAzureWebPubSubServiceClient(
"WebPubSubConnectionName",
"your_hub_name");
然后,将从 ConnectionStrings
配置部分检索连接信息。 支持两种连接格式:
使用服务终结点
建议的方法是使用与 AzureMessagingWebPubSubSettings.Credential
属性配合使用的服务终结点来建立连接。 如果未配置凭据,则使用 DefaultAzureCredential。
{
"ConnectionStrings": {
"WebPubSubConnectionName": "https://xxx.webpubsub.azure.com"
}
}
连接字符串
或者,可以使用连接字符串。
{
"ConnectionStrings": {
"WebPubSubConnectionName": "Endpoint=https://xxx.webpubsub.azure.com;AccessKey==xxxxxxx"
}
}
使用配置提供程序
.NET Aspire
Azure Web PubSub 库支持 Microsoft.Extensions.Configuration。 它使用 AzureMessagingWebPubSubSettings
密钥从配置加载 WebPubSubServiceClientOptions
和 Aspire:Azure:Messaging:WebPubSub
。 请考虑配置某些选项的示例 appsettings.json:
{
"Aspire": {
"Azure": {
"Messaging": {
"WebPubSub": {
"DisableHealthChecks": true,
"HubName": "your_hub_name"
}
}
}
}
}
使用内联委托
还可以传递 Action<AzureMessagingWebPubSubSettings> configureSettings
委托来设置一些或所有内联选项,例如禁用代码中的运行状况检查:
builder.AddAzureWebPubSubServiceClient(
"wps",
settings => settings.DisableHealthChecks = true);
您也可以使用 WebPubSubServiceClientOptions 方法的可选 Action<IAzureClientBuilder<WebPubSubServiceClient, WebPubSubServiceClientOptions>> configureClientBuilder
参数来设置 AddAzureWebPubSubHub
。 例如,若要设置此客户端的客户端 ID:
builder.AddAzureWebPubSubServiceClient(
"wps",
configureClientBuilder: clientBuilder =>
clientBuilder.ConfigureOptions(options => options.Retry.MaxRetries = 5));
健康检查
默认情况下,.NET.NET Aspire 集成将为所有服务启用 健康检查。 有关详细信息,请参阅 .NET.NET Aspire 集成概述。
当客户端成功连接到 Azure Web PubSub 服务时,.NET AspireAzure Web PubSub 集成句柄会公开可配置的运行状况检查,该检查将报告为 正常。
可观测性和遥测
.NET .NET Aspire 集成会自动设置日志记录、跟踪和指标配置,这些配置有时称为 可观测性的支柱。 有关集成可观测性和遥测的详细信息,请参阅 .NET.NET Aspire 集成概述。 根据支持服务,某些集成可能仅支持其中一些功能。 例如,某些集成支持日志记录和跟踪,但不支持指标。 也可以使用 配置 部分中介绍的技术禁用遥测功能。
伐木
.NET Aspire Azure Web PubSub 集成使用以下日志类别:
Azure
Azure.Core
Azure.Identity
Azure.Messaging.WebPubSub
描图
.NET Aspire Azure Web PubSub 集成将使用 OpenTelemetry发出以下跟踪活动:
- “Azure。Messaging.WebPubSub.*”
指标
由于 .NET Aspire SDK for Azure的限制,Azure.NET Web PubSub 集成目前不支持指标。 如果将来发生此更改,将更新此部分以反映这些更改。