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 EventHubProducerClie
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
.