My solution has a ASP.NET Web API project and a Core project that contains the entity framework core DbContext, repository (SeriesRepository : ISeriesRepository), and service that calls the repository (SeriesService : ISeriesService). The API should call the service using dependency injection by adding Builder.Services.AddScoped<ISeriesService, SeriesService>(); in Program.cs. The controller should use the SeriesService via an injected dependency:
private readonly ISeriesService _seriesService;
public SeriesController(ISeriesService seriesService)
{
_seriesService = seriesService;
}
But when I try and run it, I get this exception:
System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Project.Core.Services.ISeriesService Lifetime: Scoped ImplementationType: Project.Core.Services.SeriesService': Unable to resolve service for type 'Project.Core.Repositories.ISeriesRepository' while attempting to activate 'Project.Core.Services.SeriesService'.
If I add the repository Builder.Services.AddScoped<ISeriesRepository, SeriesRepository>(); in Program.cs, it runs successfully. But this seems like it defeats the purpose of using the SeriesService to abstract away the repository/DbContext. If this isn't the correct way, how do I correctly implement this using services/repositories and dependency injection?