azure - 无法配置 Azure 事件中心生产者

标签 azure .net-core azure-eventhub

我正在尝试 Azure 事件中心生产者的示例代码,并尝试向 Azure 事件中心发送一些消息。

事件中心及其策略已正确配置为发送和监听消息。我正在使用 Dotnet core 3.1 控制台应用程序。但是,代码不会超出 CreateBatchAsync() 调用。我尝试调试,断点没有转到下一行。尝试了 Try-catch-finally 仍然没有任何进展。请指导我在这里做错了什么。 Azure 上的事件中心显示了一些成功的传入请求。

    class Program
    {
        private const string connectionString = "<event_hub_connection_string>";
        private const string eventHubName = "<event_hub_name>";
        static async Task Main()
        {
            // Create a producer client that you can use to send events to an event hub
            await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
            {
                // Create a batch of events 
                using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();

                // Add events to the batch. An event is a represented by a collection of bytes and metadata. 
                eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("First event")));
                eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Second event")));
                eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Third event")));

                // Use the producer client to send the batch of events to the event hub
                await producerClient.SendAsync(eventBatch);
                Console.WriteLine("A batch of 3 events has been published.");
            }
        }
}

最佳答案

CreateBatchAsync 的调用将是创建与事件中心的连接的第一个需要。这表明您可能遇到连接或授权问题。

在您使用的默认配置中,默认网络超时为 60 秒,最多可以重试 3 次,重试之间会有一些回退。

因此,连接或授权失败可能需要大约 5 分钟才会出现。也就是说,大多数连接错误不符合重试条件,因此故障通常会在大约 1 分钟后出现。

为了帮助您进行调试,我建议调整默认重试策略以加快速度并更快地显示异常,以便您获得排除故障和进行调整所需的信息。 this sample 中讨论了执行此操作的选项。看起来像这样:

var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";

var options = new EventHubProducerClientOptions
{
    RetryOptions = new EventHubsRetryOptions
    {
        // Allow the network operation only 15 seconds to complete.
        TryTimeout = TimeSpan.FromSeconds(15),

        // Turn off retries        
        MaximumRetries = 0,
        Mode = EventHubsRetryMode.Fixed,
        Delay = TimeSpan.FromMilliseconds(10),
        MaximumDelay = TimeSpan.FromSeconds(1)
    }
};

await using var producer = new EventHubProducerClient(
    connectionString,
    eventHubName,
    options);

关于azure - 无法配置 Azure 事件中心生产者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71798152/

相关文章:

asp.net-core - .Net Core 如何在应用程序中的任何位置访问配置

c# - 如何提高 .NET Core Web API 的性能?

python - 从 Databricks 笔记本向 Azure Eventhubs 发送 Spark 数据帧时出现错误 (java.lang.NoSuchMethodError)

java - 将 jar 可执行文件上传到 Azure 并运行

两个应用程序之间的 Azure 用户模拟设置

python - 超时 : Access Azure blob storage from within an Azure ML experiment

c# - 如何覆盖默认 JsonConverter(在属性中指定)

azure - Azure 服务总线 SDK 与 Windows 1.1 SDK 服务总线之间的兼容性

java - 有没有办法使用Java中的Azure Functions的@EventHubTrigger来获取事件(EventHub)的标题数据?

c# - 如何使用 Azure IoT C SDK 为 AMQP 协议(protocol)设置某些系统属性,以及它们在 EventHub 中的最终位置?