Microsoft.Azure.Devices.ServiceClient
和 Microsoft.Azure.Devices.RegistryManager
都具有 ConnectFromConnectionString
和 CloseAsync
> 方法。
我们是否应该像使用其他 .NET connection-close
模式(例如 ADO.NET 连接、Redis 连接、套接字等)一样使用它们?当我使用类似的对象时,我会尝试尽快Close
或Disposable.Dispose()
它们。
访问同一个 IOT 中心时,对 Microsoft.Azure.Devices
对象执行相同的操作有什么好处和坏处?我运行的代码将各个 RegistryManager
和 ServiceClient
实例视为单例,它们在应用程序的整个生命周期(可能是几周或几个月)中使用。我们是否通过在这段时间内保持这些对象“打开”而使自己短路?
最佳答案
正如 @David R. Williamson 所指出的,以及 this discussion on GitHub ,一般建议将 RegistryManager
和 ServiceClient
注册为单例。
对于 ASP NET Core 或 Azure Functions,可以在启动时按如下方式完成:
ConfigureServices(services)
{
services.AddSingleton<RegistryManager>(RegistryManager.CreateFromConnectionString("connectionstring"));
}
关于Azure IOT 服务客户端/RegistryClient : What is the recommended frequency of CloseAsync?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45598284/