0

I have 3 services: Bot, Time Monitor and Parser.

  • Bot - receive and handle events from the user.
  • Time Monitor - check the time every minute and notifies the user if the condition is met.It also runs the Parser at a certain time.
  • Parser - parse and save data into database.

They all use DbContext and are added as scoped services.

But when I add DbContext as a scoped service, I may have an error when at the same time we receive an event from the user and check the time (and parse data).

Therefore, I add DbContext as a transient service, but they write on the internet that this can cause errors.

I can also register Bot and Parser as Transient services, but I can't register Time Monitor except as a singleton service because I need to check the status every minute.

What solution exists?

P.S sources - https://github.com/yungd1plomat/Timetable/tree/master

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
d1plomat
  • 3
  • 1
  • 1
    HostedServices have the same concept. They might be registered as transient but it is done only once in the application lifetime. So we can say they are singleton services. You need to inject `IServiceProvider` or similar service to create your own scope inside the service. – Eldar Oct 01 '22 at 08:13

0 Answers0