0

I have a .net application writing to event hub via https://learn.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.eventhubclient?view=azure-dotnet :

var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
EventHubEvent e = new EventHubEvent() { ... }; 
eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(serializer.Serialize(e))));

How does this code behave in case of : 1. eventHubName is wrong 2. connectionString has wrong SharedAccessKeyName 3. Event hub is otherwise unavailable

the documentation is rather scarce and in my testing, it appears that nothing fails. CreateFromConnectionString returns a valid 'EventHubClient' object and SendAsync just returns.

Greg Bala
  • 3,563
  • 7
  • 33
  • 43

1 Answers1

1

Update 12/18: .NET framework

If you're using .NET framework of event hubs as per this doc, it still has the same error message as .NET core of event hubs in my previous answer.

In your posted code:

var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
EventHubEvent e = new EventHubEvent() { ... }; 
eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(serializer.Serialize(e))));

The reason you cannot see error message when use a incorrect eventhub_name or incorrect SharedAccessKeyName is that when you're using the async method SendAsync, you didn't specify async / await keyword.

If you want to see the error message when using SendAsync method, you should use the following code for testing purpose:

namespace Sender
{
    class Program
    {
        static string eventHubName = "your_eventhub_name";
        static string connectionString = "your_namespace_connection_string";
        static void Main(string[] args)
        {
            SendingRandomMessages();

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

        static async void SendingRandomMessages()
        {
            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
            for(int i=0;i<5;i++)
            {
                try
                {
                    var message = Guid.NewGuid().ToString()+": ddd";
                    Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, message);
                    //eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
                    await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
                }
                catch (Exception exception)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
                    Console.ResetColor();
                }

                Thread.Sleep(200);
            }
        }

    }
}

when run this app:

1.Specify a wrong event hub name, you will receive an error: The messaging entity 'sb://your_eventhub_namespace.servicebus.windows.net/your_eventhub_Name' could not be found. Screenshot as below:

enter image description here

2.Specify a wrong SharedAccessKeyName, you will receive an error: InvalidSignature: The token has an invalid signature. Screenshot as below:

enter image description here


12/17: .NET core

The method / dll(Microsoft.ServiceBus.dll) in the link you provided is a legacy one. You should use the new nuget package Microsoft.Azure.EventHubs and it's related method for Azure event hub. For more details of sending / receiving event data, please refer to this official doc.

And if you follow the official doc to send events, then for your question:

eventHubName is wrong

If eventHub name is wrong, when execute SendAsync method, you will receive an error: Exception: Put token failed. status-code: 404, status-description: The messaging entity 'sb://your_eventhub_namespace.servicebus.windows.net/your_eventhub_Name' could not be found.

The screenshot of this error:

enter image description here

connectionString has wrong SharedAccessKeyName

If connectionString has wrong SharedAccessKeyName, when execute SendAsync method, you will receive an error: Exception: Put token failed. status-code: 401, status-description: InvalidSignature: The token has an invalid signature.

The screenshot of this error:

enter image description here

Event hub is otherwise unavailable

If Event hub is otherwise unavailable, for this case, I cannot make an event hub into unavailable status. But I'm sure it will return an error looks like "the event hub is not found." or "the event hub is unavailable."

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60