是否可以在多个请求中重用EventHubProducerClient [英] Can the EventHubProducerClient be reused across multiple request

查看:158
本文介绍了是否可以在多个请求中重用EventHubProducerClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在利用Azure SDK的服务将遥测数据记录到Azure事件中心.我的客户端应用程序调用一个API终结点,该终结点实例化EventHubProducerClient以将数据记录到事件中心.我正在管理许多api调用,并且想知道EventHubProducerClient是否可以在多个调用之间重用,还是应该在每个调用中使用新的实例.

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在应用程序的整个生命周期中都可以安全地缓存和使用,这是当您的应用程序定期或半定期发布事件时的最佳实践.在内部,生产者将管理其基础资源,并透明地尝试在不活动期间保持较低的资源使用量,并在较高使用期间管理其健康状况.

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.

在应用程序关闭时调用其CloseAsync方法将确保正确清理网络资源和其他非托管对象.

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

侧面说明: EventHubProducerClient的一个常见问题是,如果可以安全地将其视为长寿命产品,为什么它会实施IAsyncDisposable.在某些情况下,为方便起见,在发布很少且资源是应用程序关注的情况下进行此操作,部分是为了模仿HttpClient的模式.

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.

这篇关于是否可以在多个请求中重用EventHubProducerClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆