I have a windows service which uses Quartz
for scheduling the tasks. And I am trying to achieve the Dependency Injection
as by default the Quartz
doesn't support that with default Job Factory
. So I had to create a custom Job Factory
as follows.
var scheduler = await GetScheduler();
var serviceProvider = GetConfiguredServiceProvider();
scheduler.JobFactory = new CustomJobFactory(serviceProvider);
And below is my code for GetConfiguredServiceProvider()
.
private IServiceProvider GetConfiguredServiceProvider() {
var services = new ServiceCollection()
.AddScoped<IDailyJob, DailyJob>()
.AddScoped<IWeeklyJob, WeeklyJob>()
.AddScoped<IMonthlyJob, MonthlyJob>()
.AddScoped<IHelperService, HelperService>();
return services.BuildServiceProvider();
}
But on the line .AddScoped<IDailyJob, DailyJob>()
I am getting an error as
Severity Code Description Project File Line Suppression State Error CS1061 'ServiceCollection' does not contain a definition for 'AddScoped' and no accessible extension method 'AddScoped' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)
Anyone else faced this same issue?
Finally I was able to figure out the issue. The problem was that I was missing a reference for Microsoft.Extensions.DependencyInjection.Abstractions
. Usually this will be added to your packages when you install the Microsoft.Extensions.DependencyInjection
package, it seems like somehow it didn't automatically added to my solution. And after I add the Microsoft.Extensions.DependencyInjection.Abstractions
the build error was gone.
You can also try removing the package Microsoft.Extensions.DependencyInjection
and then reinstall the same and check whether it is adding the Microsoft.Extensions.DependencyInjection.Abstractions
by default.
Hope it helps.