You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Azure Event Hub的AMQP连接错误?

解决Azure Event Hub发送事件时的AMQP错误

问题详情

使用微软官方基础模板代码向Azure Event Hub发送事件,尝试过连接字符串方式,也查阅官方文档但未找到同类问题案例。代码如下:

using Azure.Identity;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using System.Text;

// number of events to be sent to the event hub
int numOfEvents = 3;

// The Event Hubs client types are safe to cache and use as a singleton for the lifetime
// of the application, which is best practice when events are being published or read regularly.
// TODO: Replace the <EVENT_HUB_NAMESPACE> and <HUB_NAME> placeholder values
EventHubProducerClient producerClient = new EventHubProducerClient(
    "<EVENT_HUB_NAMESPACE>.servicebus.windows.net",
    "<HUB_NAME>",
    new DefaultAzureCredential());

// Create a batch of events 
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();

for (int i = 1; i <= numOfEvents; i++)
{
    if (!eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes($"Event {i}"))))
    {
        // if it is too large for the batch
        throw new Exception($"Event {i} is too large for the batch and cannot be sent.");
    }
}

try
{
    // Use the producer client to send the batch of events to the event hub
    await producerClient.SendAsync(eventBatch);
    Console.WriteLine($"A batch of {numOfEvents} events has been published.");
    Console.ReadLine();
}
finally
{
    await producerClient.DisposeAsync();
}

持续收到如下AMQP错误:

Unhandled exception. System.InvalidOperationException: null TrackingId:7c3236fa44f547a5b39a4478722e674b_G10S1, SystemTracker:gateway5, Timestamp:2024-01-25T08:30:42

 ---&gt; Microsoft.Azure.Amqp.AmqpException: null TrackingId:7c3236fa44f547a5b39a4478722e674b_G10S1, SystemTracker:gateway5, Timestamp:2024-01-25T08:30:42

   at Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.OpenAmqpObjectAsync(AmqpObject target, Nullable`1 timeout, CancellationToken cancellationToken)

解决方案

  • 验证身份权限:使用DefaultAzureCredential时,确保运行代码的实体(本地用户账号、服务主体等)已被分配Azure Event Hubs Data Sender角色,角色需分配到目标Event Hub命名空间或具体Event Hub资源上。
  • 检查资源名称正确性:确认代码中<EVENT_HUB_NAMESPACE>和<HUB_NAME>已替换为实际值,无拼写错误,命名空间后缀.servicebus.windows.net格式正确。
  • 排查网络限制:AMQP协议默认使用5671端口,检查本地防火墙、企业网络代理是否允许出站流量访问该端口;若需代理,可通过EventHubProducerClientOptions配置代理信息。
  • 切换连接字符串验证:暂时改用Event Hub的Shared Access Key连接字符串,排除身份验证问题,代码修改示例:
    EventHubProducerClient producerClient = new EventHubProducerClient("你的Event Hub连接字符串");
    
  • 更新SDK版本:确保Azure.Messaging.EventHubs NuGet包为最新稳定版,旧版本可能存在已知的AMQP连接缺陷。
  • 添加重试策略:针对瞬态网络错误,配置客户端重试选项,示例代码:
    var options = new EventHubProducerClientOptions
    {
        RetryOptions = new EventHubsRetryOptions
        {
            MaxRetries = 5,
            Delay = TimeSpan.FromSeconds(2)
        }
    };
    EventHubProducerClient producerClient = new EventHubProducerClient(
        "<EVENT_HUB_NAMESPACE>.servicebus.windows.net",
        "<HUB_NAME>",
        new DefaultAzureCredential(),
        options);
    

内容的提问来源于stack exchange,提问作者snorre sövold

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 16:28:13