-
Notifications
You must be signed in to change notification settings - Fork 492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RegistryManager->HttpClientHelper creates a non-static HttClient wich is an antipattern #2420
Comments
Hi @tymtam2 and thank you for reporting the issue. I agree this could be better and we'll take a look at whether making the I suspect we expect most users to create an instance of |
I've taken a deeper look at the code and found that the So, that said, unless I have something wrong, I think it would be best for a service application to create and cache the registry manager instance. I think the most important thing is that Thoughts @tymtam2? |
@drwill-ms Thanks for looking into this. Not many thoughts :) We'll move to using a static RegistryManager. Should I raise a separate issue for the ServiceClient which uses HttpClientHelper in the same manner as RegistryManager? |
I've added the same documentation to the FWIW, your registry manager instance doesn't need to be static, but it should stick around between operations calls. If you just avoid disposing and reinitializing a new one, you should be fine. |
Good point, I was using 'static' as a shortcut to ~"not initialised->disposed often (e.g. per request)". |
Context
HttpClientHelper, created every time an instance of RegistryManager is created, creates a non-static HttpClient which is considered an antipattern.
HttpClients documenation states:
Singleton vs on demand
I could not find any documentation that makes it clear if RegistryManager should be used as a singleton (HttpClient pattern) or is
using var registryManager = ...
recommended. (If having a local variable on demand is the preferred way then it results in multiple HttpClients and this doesn't seem correct). I would have created a separate issue for the documentation to add this clarification but I couldn't find an appropriate documentation page.Code sample exhibiting the issue
The example at https://github.com/Azure-Samples/azure-iot-samples-csharp/blob/main/iot-hub/Samples/service/RegistryManagerSample/RegistryManagerSample.cs
shows having a local RegistryManager. RegistryManagerSample is used once in the application but arguably maybe RegistryManager should be static anyway to promote correct techniques. (If having a local variable on demand is the preferred way then it results in multiple HttpClients and this doesn't seem correct).
The text was updated successfully, but these errors were encountered: