如何在WebJob中指定EventHub使用者组?

时间:2017-12-19 16:28:05

标签: azure-webjobs azure-eventhub azure-webjobssdk

我正在使用WebJob绑定到EventHub,如下所述: https://github.com/Azure/azure-webjobs-sdk/wiki/EventHub-support

当webjob正在运行时,尝试在同一个集线器上运行Azure Service Bus Explorer会导致此异常:

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. 

据我所知,这是由使用相同消费者群体的2名听众(webjob& bus explorer)引起的。

所以我的问题是,如何在我的webjob中指定不同的消费者群体?

我目前的代码如下:

的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);
        }
    }

[编辑 - 加分问题]

我没有完全掌握消费者群体的使用和“时代”的事情。一个消费者群体仅限于一个接收者?

1 个答案:

答案 0 :(得分:0)

EventHubTrigger有一个可选的ConsumerGroup属性(source)。因此,基于此修改触发器如下:

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