https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-send
我正在关注有关事件中心的教程,但在使用完全相同的代码连接到事件网格时遇到问题。 有人遇到过类似的问题吗?
namespace EventHub
{
class Program
{
private const string connectionString = "Endpoint=sb://emailsevents.servicebus.windows.net/;SharedAccessKeyName=randomaccess;SharedAccessKey=<my_key>";
private const string eventHubName = "emailsevents.servicebus.windows.net";
private 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.");
}
}
}
}
我收到一个未处理的异常。 EventHubsException(ResourceNotFound)
当我到达终点线时
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
最佳答案
您提供的是事件中心命名空间,而不是您想要发布到的该命名空间下的事件中心实例的名称。
根据您创建命名空间的方式,您可能有也可能没有可供使用的事件中心。查看事件中心资源时,命名空间的事件中心名称列在 Azure 门户屏幕底部的网格中。 (在示例中,我的称为“dummy”)。如果您在该网格中没有看到任何项目,可以使用顶部菜单栏中的 + Event Hub
按钮创建一个项目。
一旦您知道要发布到的事件中心的名称,您就可以在这行代码中进行设置:
private const string eventHubName = "emailsevents.servicebus.windows.net";
关于azure - 未找到事件中心资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63616924/