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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With