Can the EventHubProducerClient be reused across multiple request

后端 未结 1 1683
情深已故
情深已故 2021-01-19 00:27

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

1条回答
  •  囚心锁ツ
    2021-01-19 00:57

    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.

    0 讨论(0)
提交回复
热议问题