Skip to content
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

Struggling with services lifetimes #498

Open
dmrzn opened this issue Oct 17, 2023 · 2 comments
Open

Struggling with services lifetimes #498

dmrzn opened this issue Oct 17, 2023 · 2 comments

Comments

@dmrzn
Copy link

dmrzn commented Oct 17, 2023

Hi Guys!

I'm using Telegram.Bot.Examples.Polling as a mechanism for my bot.
I'm trying to have a service class that is a dependency of UpdateHandler class the following way: The service should be constructed per each update handling (UpdateHandler call)

My initial attempt was to register that service as a scoped service but I found out that my scoped services live across multiple updates.

I'm looking at PollingServiceBase class and see that a scope is being created there:

    // Create new IServiceScope on each iteration.
    // This way we can leverage benefits of Scoped TReceiverService
    // and typed HttpClient - we'll grab "fresh" instance each time
    using var scope = _serviceProvider.CreateScope();
    var receiver = scope.ServiceProvider.GetRequiredService<TReceiverService>();

    await receiver.ReceiveAsync(stoppingToken);

My question is:
What does the scope span in PollingServiceBase.DoWork method?

The call of receiver.ReceiveAsync(stoppingToken) never completes: Internally it calls ITelegramBotClient.ReceiveAsync extension method
which in its turn calls DefaultUpdateReceiver.ReceiveAsync which internally is implemented using while (!cancellationToken.IsCancellationRequested) loop.

@Fedorus
Copy link
Member

Fedorus commented Oct 17, 2023

If you need scoped solution you can try: https://github.com/TgBotFramework/TgBotFramework.Template

@dmrzn
Copy link
Author

dmrzn commented Oct 18, 2023

Still would be great to know the motivation of the scope creating code I mentioned above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants