I'm trying to write a scheduled task that makes use of several of the repositories and services we've defined in our Xperience solution. I was happy to see that XByK allows injection into schedule task classes unlike K13.
The problem I'm having is that most of these services are registered with a scoped lifetime since they make use of Xperience system services that are also scoped. That's a problem here because the scheduled tasks don't run in a scoped context. All of my service calls fail:
Cannot resolve scoped service '[ServiceType]' from root provider.
Now we can inject an IServiceProvider
into our task, call CreateScope
, and call GetRequiredService
on that for our dependencies, but that makes testing hard. We could move the logic of the task into another class and call GetRequiredService
for that class as well, but it seems like tasks should just run in their own DI scope.
Is there any reason scheduled tasks aren't run in a DI scope? Or any way to override this default?
Environment
Xperience by Kentico version: 30.2.2
.NET version: 8
Execution environment: SaaS
Link to relevant Xperience by Kentico documentation