'Programmatically query Event Hub Consumer Group (and create if required)

Does anyone know how to query and create consumer groups in Azure Event Hubs using the .NET SDK. I've googled loads and can only find a way through the REST API (which I can do, but it would nicer if I can do it through the SDK). Thanks in advance



Solution 1:[1]

Does anyone know how to query and create consumer groups in Azure Event Hubs using the .NET SDK.

You could try to install this NuGet package, and as Sreeram said, we could use the NamespaceManager class to create consumer group.

var manager = NamespaceManager.CreateFromConnectionString("{your connection string}");
manager.CreateConsumerGroupIfNotExists("{eventHubPath}", "{consumergroup Name}");

After you executed the code, you will find the consumer group is created.

enter image description here

To get the consumer group, you could try to call EventHubClient.GetConsumerGroup method.

var factory = MessagingFactory.CreateFromConnectionString("{your connection string}");
var client = factory.CreateEventHubClient("{eventHubPath}"); 
EventHubConsumerGroup group = client.GetConsumerGroup("{consumergroup Name}");

Solution 2:[2]

NamespaceManager.CreateConsumerGroupIfNotExistsAsync(...)

ConsumerGroupDescription realtimeCG = nsMgr.CreateConsumerGroupIfNotExists("PartitionedStream_AKA_EventHub_Name");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fei Han
Solution 2 Sreeram Garlapati