Event Hubs .NET Framework API overview

This article summarizes some of the key Azure Event Hubs .NET Framework client APIs. There are two categories: management and run-time APIs. Run-time APIs consist of all operations needed to send and receive a message. Management operations enable you to manage an Event Hubs entity state by creating, updating, and deleting entities.

Monitoring scenarios span both management and run-time. For detailed reference documentation on the .NET APIs, see the .NET Framework, .NET Standard, and EventProcessorHost API references.

Management APIs

To perform the following management operations, you must have Manage permissions on the Event Hubs namespace:


// Create the event hub
var ehd = new EventHubDescription(eventHubName);
ehd.PartitionCount = SampleManager.numPartitions;
await namespaceManager.CreateEventHubAsync(ehd);


var ehd = await namespaceManager.GetEventHubAsync(eventHubName);

// Create a customer SAS rule with Manage permissions
ehd.UserMetadata = "Some updated info";
var ruleName = "myeventhubmanagerule";
var ruleKey = SharedAccessAuthorizationRule.GenerateRandomKey();
ehd.Authorization.Add(new SharedAccessAuthorizationRule(ruleName, ruleKey, new AccessRights[] {AccessRights.Manage, AccessRights.Listen, AccessRights.Send} )); 
await namespaceManager.UpdateEventHubAsync(ehd);


await namespaceManager.DeleteEventHubAsync("event hub name");

Run-time APIs

Create publisher

// EventHubClient model (uses implicit factory instance, so all links on same connection)
var eventHubClient = EventHubClient.Create("event hub name");

Publish message

// Create the device/temperature metric
var info = new MetricEvent() { DeviceId = random.Next(SampleManager.NumDevices), Temperature = random.Next(100) };
var data = new EventData(new byte[10]); // Byte array
var data = new EventData(Stream); // Stream 
var data = new EventData(info, serializer) //Object and serializer 
    PartitionKey = info.DeviceId.ToString()

// Set user properties if needed
data.Properties.Add("Type", "Telemetry_" + DateTime.Now.ToLongTimeString());

// Send single message async
await client.SendAsync(data);

Create consumer

// Create the Event Hubs client
var eventHubClient = EventHubClient.Create(EventHubName);

// Get the default consumer group
var defaultConsumerGroup = eventHubClient.GetDefaultConsumerGroup();

// All messages
var consumer = await defaultConsumerGroup.CreateReceiverAsync(partitionId: index);

// From one day ago
var consumer = await defaultConsumerGroup.CreateReceiverAsync(partitionId: index, startingDateTimeUtc:DateTime.Now.AddDays(-1));

// From specific offset, -1 means oldest
var consumer = await defaultConsumerGroup.CreateReceiverAsync(partitionId: index,startingOffset:-1); 

Consume message

var message = await consumer.ReceiveAsync();

// Provide a serializer
var info = message.GetBody<Type>(Serializer)

// Get a byte[]
var info = message.GetBytes(); 
msg = UnicodeEncoding.UTF8.GetString(info);

Event Processor Host APIs

These APIs provide resiliency to worker processes that may become unavailable, by distributing partitions across available workers.

// Checkpointing is done within the SimpleEventProcessor and on a per-consumerGroup per-partition basis, workers resume from where they last left off.
// Use the EventData.Offset value for checkpointing yourself, this value is unique per partition.

var eventHubConnectionString = System.Configuration.ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
var blobConnectionString = System.Configuration.ConfigurationManager.AppSettings["AzureStorageConnectionString"]; // Required for checkpoint/state

var eventHubDescription = new EventHubDescription(EventHubName);
var host = new EventProcessorHost(WorkerName, EventHubName, defaultConsumerGroup.GroupName, eventHubConnectionString, blobConnectionString);
await host.RegisterEventProcessorAsync<SimpleEventProcessor>();

// To close
await host.UnregisterEventProcessorAsync();

The IEventProcessor interface is defined as follows:

public class SimpleEventProcessor : IEventProcessor
    IDictionary<string, string> map;
    PartitionContext partitionContext;

    public SimpleEventProcessor()
        this.map = new Dictionary<string, string>();

    public Task OpenAsync(PartitionContext context)
        this.partitionContext = context;

        return Task.FromResult<object>(null);

    public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
        foreach (EventData message in messages)
            // Process messages here

        // Checkpoint when appropriate
        await context.CheckpointAsync();


    public async Task CloseAsync(PartitionContext context, CloseReason reason)
        if (reason == CloseReason.Shutdown)
            await context.CheckpointAsync();

Next steps

To learn more about Event Hubs scenarios, visit these links:

The .NET API references are here: