Перейти к содержимому

Get started with the Azure Event Hubs integrations

Это содержимое пока не доступно на вашем языке.

Azure Event Hubs logo

Azure Event Hubs is a native data-streaming service in the cloud that can stream millions of events per second, with low latency, from any source to any destination. The Aspire Azure Event Hubs integration enables you to connect to Azure Event Hubs instances from your applications.

In this introduction, you’ll see how to install and use the Aspire Azure Event Hubs integrations in a simple configuration. If you already have this knowledge, see Azure Event Hubs Hosting integration and Azure Event Hubs Client integration for full reference details.

To begin, install the Aspire Azure Event Hubs Hosting integration in your Aspire AppHost project. This integration allows you to create and manage Azure Event Hubs resources from your Aspire hosting projects:

Aspire CLI — Добавить пакет Aspire.Hosting.Azure.EventHubs
aspire add azure-eventhubs

Aspire CLI интерактивен; выберите подходящий результат поиска при запросе:

Aspire CLI — Пример вывода
Select an integration to add:
> azure-eventhubs (Aspire.Hosting.Azure.EventHubs)
> Other results listed as selectable options...

Next, in the AppHost project, create an Azure Event Hubs resource and pass it to the consuming client projects:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var eventHubs = builder.AddAzureEventHubs("event-hubs");
eventHubs.AddHub("messages");
builder.AddProject<Projects.ExampleService>()
.WithReference(eventHubs);
// After adding all resources, run the app...
builder.Build().Run();

The preceding code adds an Azure Event Hubs resource named event-hubs and an Event Hub named messages to the AppHost project. The WithReference method passes the connection information to the ExampleService project.

To get started with the Aspire Azure Event Hubs client integration, install the client integration package in the client-consuming project:

.NET CLI — Add Aspire.Azure.Messaging.EventHubs package
dotnet add package Aspire.Azure.Messaging.EventHubs

In the Program.cs file of your client-consuming project, call the AddAzureEventHubProducerClient extension method to register an EventHubProducerClient for use via the dependency injection container:

C# — Program.cs
builder.AddAzureEventHubProducerClient(connectionName: "event-hubs");

In the AppHost, when you used the WithReference method to pass an Azure Event Hubs 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 eventhubs becomes EVENTHUBS_URI.

Use the GetValue() method to obtain these environment variables in consuming projects:

C# — Obtain configuration properties
string eventHubsHost = builder.Configuration.GetValue<string>("EVENTHUBS_HOST");
string eventHubsUri = builder.Configuration.GetValue<string>("EVENTHUBS_URI");
string eventHubName = builder.Configuration.GetValue<string>("MESSAGES_EVENTHUBNAME");

Use Azure Event Hubs resources in client code

Section titled “Use Azure Event Hubs resources in client code”

You can then retrieve the client instance using dependency injection. For example, to retrieve your client from an example service:

public class ExampleService(EventHubProducerClient client)
{
// Use client...
}

For more information on how the Azure Event Hubs integrations work, see: