我正在尝试使用 Microsoft Docs 中的基本模板代码发布到 Azure 事件中心。
我也尝试过通过连接字符串进行连接,并且尝试查看文档,但我找不到其他有此问题的人。
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();
}
但我不断收到此错误消息。
Unhandled exception. System.InvalidOperationException: null TrackingId:7c3236fa44f547a5b39a4478722e674b_G10S1, SystemTracker:gateway5, Timestamp:2024-01-25T08:30:42
---> 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)
错误详细信息表明这是事件中心服务上的异常故障,而不是客户端有洞察力或影响力的故障。您最好的前进途径是提出 Azure 支持请求,因为您需要服务帮助。该消息中的跟踪 ID 和时间戳将允许他们引用此特定事件的服务日志。
客户端仅在一种情况下抛出
InvalidOperationException
- 服务响应 AMQP 错误代码“amqp:not-allowed”。 (src)
这被注释为“服务不理解如何处理请求”——这不是我们在正常情况下尝试打开连接/链接时应该看到的内容,堆栈跟踪表明正在发生这种情况。
您能够使用连接字符串和凭据进行重现似乎表明事件中心命名空间资源本身可能存在问题。
请注意,这并不表示身份验证失败 - 这些将返回 AMQP 代码“amqp:unauthorized-access”,并由客户端翻译为
UnauthorizedAccessException
。 S