azure - 如何仅通过订阅者使用来自 azure 事件中心的消息?

标签 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。是否有其他方法来消费来自事件中心的消息?

最佳答案

您可以在调试时跟踪异常并查找内部异常,因此这应该可以告诉您真正的原因是什么。我也遇到了这个愚蠢的异常,这是因为当您将 eventHubName 变量与 EventProcessorHost 一起使用时,它应该位于小写,(仅包含字母/数字和“-”,后跟字母或数字,表示不支持“--”。eventHubName也应以字母开头)

即使事件中心名称是“myEventHub123”,您的变量也必须类似于:

string eventHubName = "myeventhub123";

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

关于azure - 如何仅通过订阅者使用来自 azure 事件中心的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27564479/

相关文章:

c# - 从 Asp.Net 站点将数据发送到 EventHub

c# - 如何调试 Azure EventHub?

azure - 如何查找数据包到达 Azure IOT 中心的时间

Azure WebApi - 无法从互联网访问

azure - 在服务配置中设置应用程序 ConnectionString 而不是 Azure 中的 web.config

java - 将 Azure AD 身份验证与 AWS 中托管的现有基于 Java 的 Web 应用程序集成的可能选项?

c# - ADAL V3、Azure Data Lake、证书和 ServiceClientCredentials

azure现有VPN和网关,添加地址空间

azure - 当调用部署在 Azure 实例中的同一个 OpenAI GPT 模型时,如何单独跟踪每个程序的费用?

azure - 访问虚拟网络内的事件中心