Search code examples
c#azureazure-iot-hub

Azure IoT Hub's RegistryManager and Service Client - singletons or not?


Should IoT Hub Service SDK's RegistryManager (and ServiceClient) be used as a singleton (HttpClient pattern) or is using var registryManager = ... preferred?

More

Looking at RegistryManager's source code it creates its own non-static instance of HttpClientHelper which creates its own non-static instance of HttpClient which looks like an anti-pattern. This suggests one instance of the RegistryManager is preferred and a look at its code makes me think multiple users should be OK.

However I was not able to fine a piece of documentation that says that one instance of RegistryManager is supposed to be used as a singleton for the whole application.


Solution

  • This is now documented

        /// This client creates lifetime long instances of <see cref="HttpClient"/> that are tied to the URI of the
        /// IoT hub specified, configure any proxy settings, and connection lease timeout.
        /// For that reason, the instances are not static and an application using this client
        /// should create and save it for all use. Repeated creation may cause
        /// <see href="https://learn.microsoft.com/azure/architecture/antipatterns/improper-instantiation/">socket exhaustion</see>.
    

    https://github.com/Azure/azure-iot-sdk-csharp/pull/2423