Console Program stops after CreateBatchAsync for Event Hub

340 Views Asked by At

I'm trying to send events to an Azure Event hub by using the C# script from a Microsoft example.

However after the line ...CreateBatchAsync the program stops and never gets to the next line. What is going wrong?

this is the code from https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal

> // 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.");
}
finally
{
    await producerClient.DisposeAsync();
}
1

There are 1 best solutions below

3
On

I used the same code which you used from the MSDoc and got the below errors.

Error-1: Put token failed. status-code: 404.

Error-2: Unauthorized access. 'Send' claim(s) are required to perform this operation.

Later I modified the code as below, for the producerClient I passed only the connection string instead of hubnamespace and hub name

EventHubProducerClient producerClient = new EventHubProducerClient("ConnectionString");

Code

// 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("ConnectionString");

            // Create a batch of events 
            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.");
            }
            finally
            {
                await producerClient.DisposeAsync();
            }

And able to debug the code without any issue.

enter image description here

Result:

enter image description here

In Azure portal

enter image description here