0
votes

I have created an Event Hub Namespace and 2 event hubs. I defined a Shared Access Policy (SAP) on the Event Hub Namespace. However, when I use the connection string defined on the namespace, I am able to send events to only one of the hubs even though I create the client using the correct event hub name

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}");
    }
}

The above code is called for sending events to Hub1 and Hub2. When I use the connection string from the SAP defined on the Namespace, I can only send events to Hub1 or Hub2 whichever happens to be called first. I am specifying the eventHubName as Hub1 or Hub2 as appropriate.

I call the function SendEvent in my calling code.

The only way I can send to both hubs is to define SAP on each hub and use that connection string when creating the EventHubProducer

Am I missing something or is this by design?

1
How do you test for sending events to Hub1 and Hub2? do you mean in the first time you are using the code await using(var producerClient = new EventHubProducerClient(connectionString, Hub1)) {xx} for sending to Hub1, then in the next step, you copy the same code, just changing Hub1 to Hub2?Ivan Yang
You'd better add the completed code.Ivan Yang
@IvanYang updated code to reflect actual usagerams
Would you mind sharing your connection string, with any shared key information redacted? I'm curious if you're using the Event Hub-level connection string rather than the namespace-level.Jesse Squire
@JesseSquire I double checked the connection string. I can confirm it is not the connection string issue. The Hub level CS does not have the hub name in it.rams

1 Answers

1
votes

I did a quick test at my side, and it can work well at my side.

Please try the code below, and let me know if it does not meet your need:

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);
        }
    }

}

After running the code, I can see the data are sent to both of the 2 eventhub instances. Here is the screenshot:

enter image description here

enter image description here