Azure EventHubs in Aspire on local PC

In this exercise, I focused on only one Azure resource: Azure Event Hubs. My goal was simple: run Event Hubs locally as an emulator,
connect a console client project to it through .NET Aspire, and validate that the client can send and receive an event end-to-end.

How it looks like

This is the Aspire

How I wired Azure Event Hubs in Aspire AppHost

In AzureEmulators/AppHost.cs, I added Azure Event Hubs as an emulator, created an event hub, and added a consumer group.
Then I connected the Event Hubs client project with WithReference(eventHubs) and WaitFor(eventHubs).

  • Azure Event Hubs emulator resource: azureEventHubs1
  • Event hub: azureEventHubsHub1
  • Consumer group: azureEventHubsHubConsumer
  • Client project reference: EventHubsClient1
var eventHubs = builder.AddAzureEventHubs("azureEventHubs1")
     .RunAsEmulator()
     ;
var hub= eventHubs.AddHub("azureEventHubsHub1");
hub.AddConsumerGroup("azureEventHubsHubConsumer");
builder.AddProject<Projects.EventHubsClient>("EventHubsClient1")
        .WithReference(eventHubs)
        .WaitFor(eventHubs)
        ;

What my EventHubsClient project does

In EventHubsClient/Program.cs, I implemented a startup flow to validate Event Hubs connectivity and demonstrate event publishing and consuming.

  • I scan environment variables and pick the Event Hubs connection string.
  • I fail fast if the connection string is missing.
  • I create an EventHubProducerClient for the hub azureEventHubsHub1.
  • I send one test event to the hub.
  • I create an EventHubConsumerClient and read events from the default consumer group.
  • I print the received event body to verify the round trip.

Code from my console client project

using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Producer;
using System.Collections;
using System.Text;

Console.WriteLine("Hello, Event Hub!");
string connectionStringEventHubs = "";
foreach (var item in Environment.GetEnvironmentVariables().Cast<DictionaryEntry>())
{
    if (item.Key?.ToString()?.Contains("azureEventHubs") == true)
    {
        Console.WriteLine($"{item.Key}: {item.Value}");
        connectionStringEventHubs = item.Value?.ToString() ?? string.Empty;
    }
}
if (string.IsNullOrWhiteSpace(connectionStringEventHubs ))
{
    Console.WriteLine("Azure Event Hubs connection string is not set in environment variables.");
    return;
}
Console.WriteLine($"Connection string for Azure Event Hubs: {connectionStringEventHubs}");
Console.WriteLine("Setting up the Event Hub producer client...");

EventHubProducerClient producerClient = new(connectionStringEventHubs, "azureEventHubsHub1");
EventData eventData = new EventData("Hello,Andrei Ignat from Azure Event Hubs!");
Console.WriteLine("Sending event to Azure Event Hubs...");
await producerClient.SendAsync([eventData]);


Console.WriteLine("Receiving event from Azure Event Hubs...");
var consumer = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, connectionStringEventHubs, "azureEventHubsHub1");
await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync(new ReadEventOptions { MaximumWaitTime = TimeSpan.FromSeconds(2) }))
{
    if (partitionEvent.Data != null)
    {
        string messageBody = Encoding.UTF8.GetString(partitionEvent.Data.Body.ToArray());
        Console.WriteLine($"!!!!Message received : '{messageBody}'");
    }
    else
    {
        break;
    }
}

Output logs from my client project

Waiting for resource 'azureEventHubs1' to enter the 'Running' state.
Waiting for resource 'azureEventHubs1' to become healthy.
Waiting for resource ready to execute for 'azureEventHubs1'.
Finished waiting for resource 'azureEventHubs1'.
Hello, Event Hub!
ConnectionStrings__azureEventHubs1: Endpoint=sb://localhost:59910;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true
Connection string for Azure Event Hubs: Endpoint=sb://localhost:59910;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true
Setting up the Event Hub producer client...
Sending event to Azure Event Hubs...
Receiving event from Azure Event Hubs...
!!!!Message received : 'Hello,Andrei Ignat from Azure Event Hubs!'

What is achieved

  • I can run Azure Event Hubs on my local PC without provisioning a cloud Event Hubs namespace.
  • I get dependency wiring from Aspire between AppHost and EventHubsClient.
  • I created an event hub and consumer group locally in the emulator.
  • I proved messaging by sending an event and reading it back through the consumer client.

More links

Azure Event Hubs Get Started


Posted

in

, ,

by

Tags: