mikebridge
mikebridge

Reputation: 4585

Create an azure eventhub consumer group via net core SDK?

The older WindowsAzure.ServiceBus library has a call CreateConsumerGroupIfNotExists to create a consumer group on an azure eventhub. However, this is Net Framework 4.6 only. I'm trying to figure out how to create a consumer in netstandard2.0 from C# but I don't see an equivalent. Is there a way to do this?

Upvotes: 1

Views: 1932

Answers (2)

mikebridge
mikebridge

Reputation: 4585

Peter Pan is correct in pointing out the C# API call for this. Here are some more details:

  • Create a service principal in Azure (or use an existing one)
  • Grant this service principal "Owner" access to the Event Hubs Instance (under IAM => "Role Assignments" in the portal)

The variables in the code below come from the service client, the event hub and the subscription:

private static async Task EnsureConsumerGroup(string consumerGroupName)
{
    var context = new AuthenticationContext($"https://login.windows.net/{MY_TENANT_ID}");

    var token = await context.AcquireTokenAsync(
        "https://management.core.windows.net/",
        new ClientCredential(MY_CLIENT_ID, MY_CLIENT_SECRET)
    );
    var serviceClientCredentials = new TokenCredentials(token.AccessToken);
    var eventHubManagementClient = new EventHubManagementClient(serviceClientCredentials)
    {
        SubscriptionId = MY_SUBSCRIPTION_ID
    };

    var consumerGroupResponse = await 
eventHubManagementClient.ConsumerGroups.CreateOrUpdateWithHttpMessagesAsync(
        MY_RESOURCE_GROUP_NAME,
        MY_NAMESPACE_NAME,
        MY_EVENT_HUB_NAME,
        consumerGroupName,
        new ConsumerGroup() // I don't know what this parameter is supposed to do.
    );
}

Presumably you would handle the error condition in consumerGroupResponse.Response as well.

See also:

Upvotes: 0

Peter Pan
Peter Pan

Reputation: 24148

Generally, you can directly use the REST API Create consumer group to create EventHubs Consumer Group in any programming language.

In your scenario using C#, there is an API of Azure SDK for .NET ConsumerGroupsOperationsExtensions.CreateOrUpdate(IConsumerGroupsOperations, String, String, String, String, ConsumerGroup) Method that you can use. And according to the information from NuGet Package Manager in Visual Studio 2017, as the figure below, the related package Microsoft.Azure.Management.EventHub supports Netstandard.Library (>=1.6.1), so it should also support your current environment netstandard2.0.

enter image description here

Upvotes: 1

Related Questions