使用事件中心命名空间上定义的策略将事件发布到Azure事件中心

问题描述

我创建了一个事件中心命名空间和2个事件中心。我在事件中心命名空间上定义了共享访问策略(SAP)。但是,当我使用在命名空间上定义的连接字符串时,即使我使用正确的事件中心名称创建客户端,也只能将事件发送到其中一个中心

function void SendEvent(connectionString,eventHubName){
        await using(var producerClient = new EventHubProducerClient(connectionString,eventHubName)) {
            // Create a batch of events 
            using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
            
            var payload = GetEventModel(entity,entityName);
            // Add events to the batch. An event is a represented by a collection of bytes and metadata. 
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(payload.ToString())));
            
            // Use the producer client to send the batch of events to the event hub
            await producerClient.SendAsync(eventBatch);
            
            System.Diagnostics.Debug.WriteLine($"Event for {entity} sent to Hub {eventHubName}");
    }
}

调用上面的代码将事件发送到Hub1和Hub2。当我使用命名空间上定义的来自SAP的连接字符串时,我只能将事件发送到Hub1Hub2,以先碰到的那个为准。我将eventHubName指定为Hub1Hub2

我在调用代码中调用了函数SendEvent

我可以发送到两个集线器的唯一方法是在每个集线器上定义SAP并在创建EventHubProducer时使用该连接字符串

我错过了什么吗?或者这是设计使然?

解决方法

我在我的身边做了一个快速测试,它可以很好地在我这边工作。

请尝试以下代码,如果不满足您的要求,请通知我:

class Program
{
    //the namespace level sas
    private const string connectionString = "Endpoint=sb://yyeventhubns.servicebus.windows.net/;SharedAccessKeyName=mysas;SharedAccessKey=xxxx";

    //I try to send data to the following 2 eventhub instances.
    private const string hub1 = "yyeventhub1";
    private const string hub2 = "yyeventhub2";

    static async Task Main()
    {
        SendEvent(connectionString,hub1);
        SendEvent(connectionString,hub2);

        Console.WriteLine("**completed**");
        Console.ReadLine();
    }

    private static async void SendEvent(string connectionString,string eventHubName)
    {
        // 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: "+eventHubName)));
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Second event: "+eventHubName)));
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Third event: "+eventHubName)));
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Fourth event: " + eventHubName)));
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Fifth event: " + eventHubName)));

            // 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 to: "+ eventHubName);
        }
    }

}

运行代码后,我可以看到数据已发送到两个eventhub实例。这是屏幕截图:

enter image description here

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...