无法配置 Azure 事件中心生产者
Unable to configure Azure Event Hub Producer
我正在尝试 Azure Event Hub Producer 的示例代码并尝试向 Azure Event Hub 发送一些消息。
eventhub 及其策略已正确配置以发送和侦听消息。我正在使用 Dotnet 核心 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 次,中间有 back-off 次重试。
因此,连接或授权失败最多可能需要大约 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 Event Hub Producer 的示例代码并尝试向 Azure Event Hub 发送一些消息。
eventhub 及其策略已正确配置以发送和侦听消息。我正在使用 Dotnet 核心 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 次,中间有 back-off 次重试。
因此,连接或授权失败最多可能需要大约 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);