Telegram.Bot icon indicating copy to clipboard operation
Telegram.Bot copied to clipboard

Make TelegramBotClient disposable

Open UselessToucan opened this issue 6 years ago • 3 comments

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.

UselessToucan avatar Nov 09 '19 20:11 UselessToucan

This will make the client not DI-friendly since disposing of dependencies is a responsibility of a scope in a DI framework.

tuscen avatar Nov 10 '19 11:11 tuscen

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 HttpClient when TelegramBotClient is a singleton object
  • we might consider disposing constructor-instantiated HttpClient when TelegramBotClient is a transient object
  • maybe we should encourage users to instantiate HttpClient for us (either as singleton or via HttpClientFactory) leaving only constructor which accepts both botToken and HttpClient.

karb0f0s avatar Sep 23 '20 20:09 karb0f0s

@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.

tuscen avatar Sep 24 '20 16:09 tuscen