Andrea Cattaneo Andrea Cattaneo - 20 days ago 15
C# Question

Is it correct to create EventHubClient for every message I send?

I'm creating an EventHub client application that sends message to an Event Hub on Azure.
Now when I have a message to send (every 5-6 seconds) I create a new instance of the class EventHubClient, then I use it to send data:

public async static void SendListOfMeasurements(string eventHubName, string connectionString, List<Measurement> measurementsList, int maxMessageSize)
{
// Create EventHubClient
EventHubClient client = EventHubClient.CreateFromConnectionString(
connectionString,
eventHubName);
...


Is it a good practice? Or is it better to create it only at startup and then use only the .Send method?
What is the best scenario in terms of performance? In the future the ammount of Sends could increase and also the quantity of messages

Answer

No, It's necessary to create a new EventHubClient using the MessageFactory only the first time. If I create a Client for every message I send, then a new connection is created and the old one remain opened (checked using netstat) resulting in a lot of "ESTABILISHED" connections.

Using a single client created for the first message, then recycling the client for the other messages, if the internet connection is lost then comes back, the tcp connection is automatically re-created.

Comments