I am creating a scheduled task with Quartz.Net in Asp.Net Core 5.0, I had already done it in later versions and all is well with this example from the following link:
https://andrewlock.net/creating-a-quartz-net-hosted-service-with-asp-net-core/
But now it gives me the following error the moment the task is invoked:
Cannot resolve 'IntegracionDocu.Commons.QuartzDescargaProgramada' from root provider because it requires scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'.
(“I understand that I must create a scoped service for the singleton to consume”) I DON'T KNOW HOW TO DO IT??
I found this that should go in the QuartzDownloadScheduled class but I don't know where theIScopedService
public Task Execute(IJobExecutionContext context)
{
// Create a new scope
using(var scope = _provider.CreateScope())
{
// Resolve the Scoped service
var service = scope.ServiceProvider.GetService<IScopedService>();
_logger.LogInformation("Ejecutandose!");
}
return Task.CompletedTask;
}
}
My code
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Agregamos servicio Quartz
services.AddSingleton<IJobFactory, SingletonJobFactory>();
services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
// Agregamos nuestro trabajo programado
services.AddSingleton<QuartzDescargaProgramada>();
services.AddSingleton(new JobSchedule(jobType: typeof(QuartzDescargaProgramada), cronExpression: "0 54 22 ? * *"));
services.AddHostedService<QuartzHostedService>();
}
That is only if you want to use some other Scoped service within the Job.
In this case, you don't mention which Scoped service you want to use, also, you don't have any Scoped registered, so I don't think it's necessary in your case.
Instead of IScopedService, the interface or implementation of the Scoped service that you are going to use within your Job should go.
An example would be if you had a Scoped service
ValueService
:and you would like to use it in your Job: