Search code examples
azure-eventhubazure-sdk-.net

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

  • 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}");