Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the EventHubProducerClient be reused across multiple request

I'm leveraging services of the Azure SDK to log telemetry data to Azure Event Hubs. My client application calls an API endpoint which instantiates the EventHubProducerClient to log data to Event Hubs. I am managing many api calls and was wondering if the EventHubProducerClient can be reused across multiple calls or should I use a new instance per call.

like image 309
Mike Check Avatar asked Aug 31 '20 18:08

Mike Check


People also ask

What is EventHubProducerClient?

EventHubProducerClient(string, EventHubClientOptions) The EventHubProducerClient class is used to send events to an Event Hub. Use the options parmeter to configure retry policy or proxy settings.

Which .NET Framework class contains the EventHubClient?

NET managed APIs, the primary constructs for publishing data to Event Hubs are the EventHubClient and EventData classes. EventHubClient provides the AMQP communication channel over which events are sent to the event hub. The EventData class represents an event, and is used to publish messages to an event hub.


1 Answers

Yes. The EventHubProducerClient is safe to cache and use for the lifetime of the application, and that is the best practice for use when your application publishes events regularly or semi-regularly. Internally, the producer will manage it's underlying resources and transparently attempt to keep resource usage low during periods of inactivity and manage their health during periods of higher use.

Calling its CloseAsync method as your application is shutting down will ensure that network resources and other unmanaged objects are properly cleaned up.

Side Note: A common question for the EventHubProducerClient is why does it implement IAsyncDisposable if it is safe to treat as long-lived. This was done partially for convenience in scenarios where publishing is very infrequent and resources are a concern for the application, and partially to mimic the pattern of the HttpClient.

like image 133
Jesse Squire Avatar answered Oct 22 '22 15:10

Jesse Squire