Aspire.Azure.Messaging.EventHubs 8.0.0-preview.6.24214.1

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
This is a prerelease version of Aspire.Azure.Messaging.EventHubs.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Aspire.Azure.Messaging.EventHubs --version 8.0.0-preview.6.24214.1
NuGet\Install-Package Aspire.Azure.Messaging.EventHubs -Version 8.0.0-preview.6.24214.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aspire.Azure.Messaging.EventHubs" Version="8.0.0-preview.6.24214.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aspire.Azure.Messaging.EventHubs --version 8.0.0-preview.6.24214.1
#r "nuget: Aspire.Azure.Messaging.EventHubs, 8.0.0-preview.6.24214.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Aspire.Azure.Messaging.EventHubs as a Cake Addin
#addin nuget:?package=Aspire.Azure.Messaging.EventHubs&version=8.0.0-preview.6.24214.1&prerelease

// Install Aspire.Azure.Messaging.EventHubs as a Cake Tool
#tool nuget:?package=Aspire.Azure.Messaging.EventHubs&version=8.0.0-preview.6.24214.1&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

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 PartitionReceiverare 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 Namespace 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",
            "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.AddAzureEventHubs("eventHubsConnectionName").AddEventHub("MyHub");;

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(eventHubs);

The AddAzureEventHubs method will 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

Feedback & contributing

https://github.com/dotnet/aspire

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
8.0.0-preview.7.24251.11 27 5/7/2024
8.0.0-preview.6.24214.1 55 4/23/2024
8.0.0-preview.5.24201.12 60 4/9/2024