Aspire.Azure.Messaging.EventHubs
8.0.0-preview.8.24258.2
Prefix Reserved
See the version list below for details.
dotnet add package Aspire.Azure.Messaging.EventHubs --version 8.0.0-preview.8.24258.2
NuGet\Install-Package Aspire.Azure.Messaging.EventHubs -Version 8.0.0-preview.8.24258.2
<PackageReference Include="Aspire.Azure.Messaging.EventHubs" Version="8.0.0-preview.8.24258.2" />
paket add Aspire.Azure.Messaging.EventHubs --version 8.0.0-preview.8.24258.2
#r "nuget: Aspire.Azure.Messaging.EventHubs, 8.0.0-preview.8.24258.2"
// Install Aspire.Azure.Messaging.EventHubs as a Cake Addin #addin nuget:?package=Aspire.Azure.Messaging.EventHubs&version=8.0.0-preview.8.24258.2&prerelease // Install Aspire.Azure.Messaging.EventHubs as a Cake Tool #tool nuget:?package=Aspire.Azure.Messaging.EventHubs&version=8.0.0-preview.8.24258.2&prerelease
Aspire.Azure.Messaging.EventHubs
Offers options for registering an EventHubProducerClient, an EventHubConsumerClient, an EventProcessorClient or a PartitionReceiver in the DI container for connecting to Azure Event Hubs.
Getting started
Prerequisites
- Azure subscription - create one for free
- Azure Event Hubs namespace, learn more about how to add an Event Hubs namespace. Alternatively, you can use a connection string, which is not recommended in production environments.
Install the package
Install the .NET Aspire Azure Event Hubs library with NuGet:
dotnet add package Aspire.Azure.Messaging.EventHubs
Supported clients with Options classes
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 |
EventProcessorClient | EventProcessorClientOptions | AzureMessagingEventHubsProcessorSettings |
PartitionReceiver | PartitionReceiverOptions | AzureMessagingEventHubsPartitionReceiverSettings |
Usage example
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 PartitionReceiver
are configured in a similar manner.
In the Program.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
.
Configuration
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.
Use a connection string
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:
Fully Qualified Namespace
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"
}
}
Connection string
Alternatively, use a connection string:
{
"ConnectionStrings": {
"eventHubsConnectionName": "Endpoint=sb://mynamespace.servicebus.windows.net/;SharedAccessKeyName=accesskeyname;SharedAccessKey=accesskey;EntityPath=MyHub"
}
}
Use configuration providers
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"));
AppHost extensions
In your AppHost project, install the Aspire Azure Event Hubs Hosting library with NuGet:
dotnet add package Aspire.Hosting.Azure.EventHubs
Then, in the Program.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").AddEventHub("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 AddEventHub
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";
});
Additional documentation
- https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/eventhub/Microsoft.Azure.EventHubs/README.md
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
Feedback & contributing
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Azure.Identity (>= 1.11.2)
- Azure.Messaging.EventHubs.Processor (>= 5.11.2)
- Microsoft.Extensions.Azure (>= 1.7.3)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.1)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- OpenTelemetry.Extensions.Hosting (>= 1.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
9.0.0 | 203 | 11/12/2024 |
9.0.0-rc.1.24511.1 | 341 | 10/15/2024 |
8.2.2 | 1,204 | 10/24/2024 |
8.2.1 | 2,293 | 9/26/2024 |
8.2.0 | 1,630 | 8/29/2024 |
8.1.0-preview.1.24373.2 | 1,705 | 7/23/2024 |
8.0.2-preview.1.24326.4 | 111 | 6/28/2024 |
8.0.1-preview.8.24267.1 | 251 | 5/21/2024 |
8.0.0-preview.8.24258.2 | 59 | 5/21/2024 |
8.0.0-preview.7.24251.11 | 118 | 5/7/2024 |
8.0.0-preview.6.24214.1 | 79 | 4/23/2024 |
8.0.0-preview.5.24201.12 | 88 | 4/9/2024 |