Getting started with the Azure Web PubSub integration
Цей контент ще не доступний вашою мовою.
Azure Web PubSub is a fully managed real-time messaging service that enables you to build real-time web applications using WebSockets and publish-subscribe patterns. The Aspire Azure Web PubSub Hosting integration provides methods to create Azure Web PubSub resources from code in your Aspire AppHost project.
In this introduction, you’ll see how to install and use the Aspire Azure Web PubSub integrations in a simple configuration. If you already have this knowledge, see Azure Web PubSub Hosting integration for full reference details.
Set up hosting integration
Section titled “Set up hosting integration”To begin, install the Aspire Azure Web PubSub Hosting integration in your Aspire AppHost project. This integration allows you to create and manage Azure Web PubSub resources from your Aspire hosting projects:
aspire add azure-webpubsubAspire CLI інтерактивний; оберіть відповідний результат пошуку:
Select an integration to add:
> azure-webpubsub (Aspire.Hosting.Azure.WebPubSub)> Other results listed as selectable options...#:package Aspire.Hosting.Azure.WebPubSub@*<PackageReference Include="Aspire.Hosting.Azure.WebPubSub" Version="*" />Next, in the AppHost project, create an Azure Web PubSub resource and pass it to the consuming client projects:
var builder = DistributedApplication.CreateBuilder(args);
var webPubSub = builder.AddAzureWebPubSub("web-pubsub");
var myService = builder.AddProject<Projects.MyService>() .WithReference(webPubSub);
// After adding all resources, run the app...
builder.Build().Run();The preceding code adds an Azure Web PubSub resource named web-pubsub to the AppHost project and passes the connection information to the consuming project.
Set up client integration
Section titled “Set up client integration”To use Azure Web PubSub from your client applications, install the Aspire Azure Web PubSub client integration in your client project:
dotnet add package Aspire.Azure.Messaging.WebPubSub#:package Aspire.Azure.Messaging.WebPubSub@*<PackageReference Include="Aspire.Azure.Messaging.WebPubSub" Version="*" />In the Program.cs file of your client-consuming project, call the AddAzureWebPubSubServiceClient extension method to register a WebPubSubServiceClient for use via the dependency injection container:
builder.AddAzureWebPubSubServiceClient(connectionName: "web-pubsub");Use injected Azure Web PubSub properties
Section titled “Use injected Azure Web PubSub properties”In the AppHost, when you used the WithReference method to pass an Azure Web PubSub resource to a consuming client project, Aspire injects several configuration properties that you can use in the consuming project.
Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called web-pubsub becomes WEB_PUBSUB_URI.
Use the GetValue() method to obtain these environment variables in consuming projects:
string webPubSubUri = builder.Configuration.GetValue<string>("WEB_PUBSUB_URI");Add Azure Web PubSub resources in client code
Section titled “Add Azure Web PubSub resources in client code”After adding the WebPubSubServiceClient, you can retrieve the connection instance using dependency injection:
public class ExampleService(WebPubSubServiceClient client){ // Use client...}For full details on using the client integration, see Azure Web PubSub Client integration.