0

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();
}
Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22
Ellen
  • 1
  • 3
  • 1
    Have you tried catching the error? – Radu Hatos Apr 06 '23 at 11:55
  • Please add try catch around using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); and add a new breakpoint in exception and follow the message :) – Mehdi Kacim Apr 06 '23 at 11:55
  • It doesn't give an error. It just stops. – Ellen Apr 06 '23 at 12:18
  • Similar issue faced when sending a message to a Azure Service Bus Topic, no errors, no exception, sample program from github just ends, weird. Rebuilt the VS solution, created fresh clone, no luck. OP, hope you have replaced the names of hub and NS. – Anand Sowmithiran Apr 06 '23 at 13:11
  • I did. I would have gotten an error message otherwise. – Ellen Apr 06 '23 at 14:44

1 Answers1

0

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

Rajesh Mopati
  • 1,329
  • 1
  • 2
  • 7
  • The errors 1 and 2 went away just by dropping the Hubname and only passing connection string, or did you do some other change? – Anand Sowmithiran Apr 10 '23 at 10:25
  • I passed only the connection string. And it worked for me. – Rajesh Mopati Apr 10 '23 at 10:43
  • I've tried this and see in the chart that messages have arrived, but when I go to the event hub en choose Process Data and Process your Event Hub data using Stream Analytics Query Language, then it says that there is no data. – Ellen Apr 12 '23 at 13:44