Search code examples
azureazure-webjobsazure-eventhubazure-webjobssdk

How to specify EventHub Consumer Group in a WebJob?


I am using WebJob's bindings to EventHub as described here: https://github.com/Azure/azure-webjobs-sdk/wiki/EventHub-support

While the webjob is running, trying to run the Azure Service Bus Explorer on the same hub result in this exception:

Exception: A receiver with a higher epoch '14' already exists. A new receiver with epoch 0 cannot be created. 
Make sure you are creating receiver with increasing epoch value to ensure connectivity, or ensure all old epoch receivers are closed or disconnected. 

From what I understand, this is caused by the 2 listeners(webjob & bus explorer) using the same Consumer Group.

So my question, how can I specify a different Consumer Group in my webjob ?

My current code look like this:

Program.cs:

var config = new JobHostConfiguration()
{
    NameResolver = new NameResolver()
};

string eventHubConnectionString = ConfigurationManager.ConnectionStrings["EventHub"].ConnectionString;
string eventHubName = ConfigurationManager.AppSettings["EventHubName"];
string eventProcessorHostStorageConnectionString = ConfigurationManager.ConnectionStrings["EventProcessorHostStorage"].ConnectionString; ;

var eventHubConfig = new EventHubConfiguration();
eventHubConfig.AddReceiver(eventHubName, eventHubConnectionString, eventProcessorHostStorageConnectionString);
config.UseEventHub(eventHubConfig);

var host = new JobHost(config);

host.RunAndBlock();

Functions.cs:

public class Functions
    {
        public static void Trigger([EventHubTrigger("%EventHubName%")] string message, TextWriter log)
        {
            log.WriteLine(message);
        }
    }

[Edit - Bonus Question]

I don't fully grasp the use of Consumer Group and 'epoch' thing. One Consumer Group is limited to one receiver ?


Solution

  • The EventHubTrigger has an optional ConsumerGroup property (source). So, based on that modify the trigger like this:

        public class Functions
        {
            public static void Trigger([EventHubTrigger("%EventHubName%", ConsumerGroup = "MyConsumerGroup")] string message, TextWriter log)
            {
                log.WriteLine(message);
            }
        }