Make TelegramBotClient disposable
As far as I see, there is a HttpClient instance creation under the hood of a TelegramBotClient. Since HttpClient is IDisposable, I guess we should call _httpClient.Dispose() at some point.
This will make the client not DI-friendly since disposing of dependencies is a responsibility of a scope in a DI framework.
As mentioned in various resources - HttpClient should not be disposed after each request. But, it should be disposed at the end of its owner's lifecycle.
Summarizing:
- we should not dispose user-provided
HttpClient - we might not care about constructor-instantiated
HttpClientwhenTelegramBotClientis a singleton object - we might consider disposing constructor-instantiated
HttpClientwhenTelegramBotClientis a transient object -
maybe we should encourage users to instantiate
HttpClientfor us (either as singleton or viaHttpClientFactory) leaving only constructor which accepts bothbotTokenandHttpClient.
@karb0f0s Good points. It’s a complicated case with no single best strategy and I still don’t know what to do here.
On one hand forcing people to pass HttpClient manually every time will make using the client quite inconvenient. On the other, though, it will make it clearer who owns the client and who’s responsible for it’s disposal.