如何使用只有订阅者来自azure事件中心的消息?

时间:2014-12-19 10:50:37

标签: azure azure-eventhub

我将消息发送到azure事件中心。但我无法从事件中心下载消息。

enter code here
string eventHubConnectionString = "<connection string>";
string eventHubName = "<event Hub name>";
string storageAccountName = "<event hub storage>";
string storageAccountKey = "<storage Key>";
string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",storageAccountName, storageAccountKey);


EventProcessorHost eventProcessorHost = new EventProcessorHost("message", eventHubName,  EventHubConsumerGroup.DefaultGroupName, eventHubConnectionString, storageConnectionString);
          eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>().Wait();


IEventProcessor:
enter code here
class SimpleEventProcessor : IEventProcessor

{


    Stopwatch checkpointStopWatch;

    async Task IEventProcessor.CloseAsync(PartitionContext context, CloseReason reason)
    {
        Console.WriteLine(string.Format("Processor Shuting Down.  Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason.ToString()));
        if (reason == CloseReason.Shutdown)
        {
            await context.CheckpointAsync();
        }
    }

    Task IEventProcessor.OpenAsync(PartitionContext context)
    {
        Console.WriteLine(string.Format("SimpleEventProcessor initialize.  Partition: '{0}', Offset: '{1}'", context.Lease.PartitionId, context.Lease.Offset));
        this.checkpointStopWatch = new Stopwatch();
        this.checkpointStopWatch.Start();
        return Task.FromResult<object>(null);
    }

    async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
    {
        foreach (EventData eventData in messages)
        {
            string data = Encoding.UTF8.GetString(eventData.GetBytes());

            Console.WriteLine(string.Format("Message received.  Partition: '{0}', Data: '{1}'",
                context.Lease.PartitionId, data));
        }

        //Call checkpoint every 5 minutes, so that worker can resume processing from the 5 minutes back if it restarts.
        if (this.checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(5))
        {
            await context.CheckpointAsync();
            lock (this)
            {
                this.checkpointStopWatch.Reset();
            }
        }
    }

}

显示以下错误 聚合异常处理。发生一个或多个错误。 消息详细信息:没有这样的主机 什么是EventProcessor主机名?

它在此行显示错误:eventProcessorHost.RegisterEventProcessorAsync()。Wait();

它不是在调用IEventprocessor。是否有任何其他方法来消费来自事件中心的消息?

2 个答案:

答案 0 :(得分:2)

你可以跟踪你的异常,并在调试时查找内部异常,这样可以给你一个真正原因的线索..我也有这个愚蠢的异常,那是因为当你使用eventProcessorHost的eventHubName变量时它应该在小写,(仅包含字母/数字和&#39; - &#39;必须后跟字母或数字,这意味着不支持&#39; - &#39; .eventHubName也应该以字母开头)

即使事件中心名称是&#34; myEventHub123&#34;你的变量必须像:

string eventHubName = "myeventhub123";

希望这会对某人有所帮助..

答案 1 :(得分:0)

我使用位于here的示例代码成功构建了一个事件处理器。

很难从示例代码中判断出错误是什么,因为它可能与您的连接字符串/ eventhub /存储帐户名中的拼写错误有关,因为它没有提供(您做对了,不要发布您的敏感数据的连接字符串)。

示例从连接字符串加载事件中心信息的方式与您提供的代码的方式之间的区别在于如何通过Evenhub客户端提供信息。尝试更新您构建EventProcessorHost的方式,如下例所示:

    EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(eventHubConnectionString, this.eventHubName); 

    // Get the default Consumer Group 
    defaultConsumerGroup = eventHubClient.GetDefaultConsumerGroup(); 
    string blobConnectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; // Required for checkpoint/state 
    eventProcessorHost = new EventProcessorHost("singleworker", eventHubClient.Path, defaultConsumerGroup.GroupName, this.eventHubConnectionString, blobConnectionString); 
    eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>().Wait();