ServiceCollection does not contain a definition for AddScoped and no accessible extension method accepting a first argument of type could be found
Asked Answered
M

1

13

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?

Mchale answered 1/8, 2019 at 14:11 Comment(3)
hmmm, well, are you missing a using directive or an assembly reference?Towage
I feel like there should be a duplicate but I couldn't find one, so +1 (although a trivial fix)Tenace
Yes, I also thought initially, then I could not find one. That is why I posted it. ThanksMchale
M
23

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.

enter image description here

Hope it helps.

Mchale answered 1/8, 2019 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.