A client for Azure Event Hubs that integrates with Aspire, including logging and telemetry.
$ dotnet add package Aspire.Azure.Messaging.EventHubsOffers options for registering an EventHubProducerClient, an EventHubConsumerClient, an EventHubBufferedProducerClient, an EventProcessorClient or a PartitionReceiver in the DI container for connecting to Azure Event Hubs.
Install the .NET Aspire Azure Event Hubs library with NuGet:
dotnet add package Aspire.Azure.Messaging.EventHubs
The following clients are supported by the library, along with their corresponding Options and Settings classes:
| Client Type | Options Class | Settings Class |
|---|---|---|
| EventHubProducerClient | EventHubProducerClientOptions | AzureMessagingEventHubsProducerSettings |
| EventHubConsumerClient | EventHubConsumerClientOptions | AzureMessagingEventHubsConsumerSettings |
| EventHubBufferedProducerClient | EventHubBufferedProducerClientOptions | AzureMessagingEventHubsBufferedProducerSettings |
| EventProcessorClient | EventProcessorClientOptions | AzureMessagingEventHubsProcessorSettings |
| PartitionReceiver | PartitionReceiverOptions | AzureMessagingEventHubsPartitionReceiverSettings |
The following example assumes that you have an Azure Event Hubs namespace and an Event Hub created and wish to configure an EventHubProducerClient to send events to the Event Hub. The EventHubConsumerClient, EventProcessorClient, and PartitionReceiverare configured in a similar manner.
In the AppHost.cs file of your project, call the AddAzureEventHubProducerClient extension method to register
a EventHubProducerClient for use via the dependency injection container. The method takes a connection name parameter. This assumes you have included the EntityPath in the connection string to specify the Event Hub name.
builder.AddAzureEventHubProducerClient("eventHubsConnectionName");
Retrieve the EventHubProducerClient instance using dependency injection. For example, to retrieve the
client from a Web API controller:
private readonly EventHubProducerClient _client;
public ProductsController(EventHubProducerClient client)
{
_client = client;
}
See the Azure.Messaging.EventHubs documentation for examples on using the EventHubProducerClient.
The .NET Aspire Azure Event Hubs library provides multiple options to configure the Azure Event Hubs connection based on the requirements and conventions of your project. Note that either a FullyQualifiedNamespace or a ConnectionString is a required to be supplied.
When using a connection string from the ConnectionStrings configuration section, provide the name of the connection string when calling builder.AddAzureEventHubProducerClient() and other supported Event Hubs clients. In this example, the connection string does not include the EntityPath property, so the EventHubName property must be set in the settings callback:
builder.AddAzureEventHubProducerClient("eventHubsConnectionName",
settings =>
{
settings.EventHubName = "MyHub";
});
And then the connection information will be retrieved from the ConnectionStrings configuration section. Two connection formats are supported:
The recommended approach is to use a fully qualified namespace, which works with the AzureMessagingEventHubsSettings.Credential property to establish a connection. If no credential is configured, the DefaultAzureCredential is used.
{
"ConnectionStrings": {
"eventHubsConnectionName": "{your_namespace}.servicebus.windows.net"
}
}
Alternatively, use a connection string:
{
"ConnectionStrings": {
"eventHubsConnectionName": "Endpoint=sb://mynamespace.servicebus.windows.net/;SharedAccessKeyName=accesskeyname;SharedAccessKey=accesskey;EntityPath=MyHub"
}
}
The .NET Aspire Azure Event Hubs library supports Microsoft.Extensions.Configuration. It loads the AzureMessagingEventHubsSettings and the associated Options, e.g. EventProcessorClientOptions, from configuration by using the Aspire:Azure:Messaging:EventHubs: key prefix, followed by the name of the specific client in use. Example appsettings.json that configures some of the options for an EventProcessorClient:
{
"Aspire": {
"Azure": {
"Messaging": {
"EventHubs": {
"EventProcessorClient": {
"EventHubName": "MyHub",
"BlobContainerName": "checkpoints",
"ClientOptions": {
"Identifier": "PROCESSOR_ID"
}
}
}
}
}
}
}
You can also setup the Options type using the optional Action<IAzureClientBuilder<EventProcessorClient, EventProcessorClientOptions>> configureClientBuilder parameter of the AddAzureEventProcessorClient method. For example, to set the processor's client ID for this client:
builder.AddAzureEventProcessorClient("eventHubsConnectionName",
configureClientBuilder: clientBuilder => clientBuilder.ConfigureOptions(
options => options.Identifier = "PROCESSOR_ID"));
In your AppHost project, install the Aspire Azure Event Hubs Hosting library with NuGet:
dotnet add package Aspire.Hosting.Azure.EventHubs
Then, in the AppHost.cs file of AppHost, add an Event Hubs connection and an Event Hub resource and consume the connection using the following methods:
var eventHubs = builder.ExecutionContext.IsPublishMode
? builder.AddAzureEventHubs("eventHubsConnectionName").WithHub("MyHub")
: builder.AddConnectionString("eventHubsConnectionName");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(eventHubs);
The AddAzureEventHubs method adds an Azure Event Hubs Namespace resource to the builder. Or AddConnectionString can be used to read connection information from the AppHost's configuration (for example, from "user secrets") under the ConnectionStrings:eventHubsConnectionName config key. The WithReference method passes that connection information into a connection string named eventHubsConnectionName in the MyService project.
NOTE: Even though we are creating an Event Hub using the WithHub at the same time as the namespace, for this release of Aspire, the connection string will not include the EntityPath property, so the EventHubName property must be set in the settings callback for the preferred client. Future versions of Aspire will include the EntityPath property in the connection string and will not require the EventHubName property to be set in this scenario.
In the Program.cs file of MyService, the connection can be consumed using by calling of the supported Event Hubs client extension methods:
builder.AddAzureEventProcessorClient("eventHubsConnectionName", settings =>
{
settings.EventHubName = "MyHub";
});