如何在ServiceBus.Messaging中使用带有EventProcessorHost的工厂?

时间:2018-11-01 21:08:33

标签: c# azure-eventhub

我正试图让我的应用程序在向一系列eventhub发送消息或从一系列eventhub发送消息时重用tcp连接。

我已经通过对发件人使用messsagingfactory来做到这一点。但是我不确定如何为接收者完成同样的工作。

这是我的代码:

    private void SetupAcknowledgmentEventHub()
    {   

        // Initialize the Event Hub sender 
        var acknowledgmentConnectionString = AppSettings.AcknowledgmentConnectionString;
        var acknowledgmentEntityPath = AppSettings.AcknowledgmentEntityPath;
        var storageAccountName = AppSettings.StorageAccountName;
        var storageAccountKey = AppSettings.StorageAccountKey;
        var storageConnectionString =
            $"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey}";
        var eventProcessorHostName = Guid.NewGuid().ToString();

        var client = _messagingFactory.CreateEventHubClient(acknowledgmentEntityPath);

        var consumerGroup = client.GetConsumerGroup(AppSettings.AcknowledgmentConsumerGroup);
        if (consumerGroup == null)
        {
            var message = $"Consumer group {AppSettings.AcknowledgmentConsumerGroup} does not exist";
            Logger.Log.Error(message);
            throw new Exception(message);
        }

        // Initialize the Event Hub receiver
        var eventProcessorHost = new EventProcessorHost(
            eventProcessorHostName,
            acknowledgmentEntityPath,
            consumerGroup.GroupName,
            acknowledgmentConnectionString,
            storageConnectionString,
            "checkpoint1");

        var options = new EventProcessorOptions()
        {
            MaxBatchSize = 200000,
            PrefetchCount = 300
        };
        options.ExceptionReceived += (sender, e) =>
        {
            Logger.Log.Error($"Error {e.Exception} received while trying {e.Action}");
        };
        eventProcessorHost.RegisterEventProcessorAsync<AcknowledgementEventProcessor>(options).Wait();
    }

EventProcessorHost将Func接受到其构造函数中-但是我不太确定如何实现此功能?

应该怎么做?

0 个答案:

没有答案