I study how to write API in .NET 8. I created data layer as a service, but when I try to add service with AddScoped
I'm getting runtime exception. How to solve it?
System.AggregateException: 'Some services are not able to be constructed
(Error while validating the service descriptor 'ServiceType: TodoApiDb.Data.ITodoItemsService Lifetime:
Scoped ImplementationType: TodoApiDb.Data.TodoItemsService':
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet`1[TodoApiDb.Models.TodoItem]'
while attempting to activate 'TodoApiDb.Data.TodoItemsService'.)'
Program.cs
:
builder.Services.AddScoped<ITodoItemsService, TodoItemsService>();
ITodoItemsService.cs
:
public interface ITodoItemsService
{
List<TodoItem>? GetAbunch(string[] myAnimals);
}
TodoItemsService.cs
:
public class TodoItemsService: ITodoItemsService
{
private readonly DbSet<TodoItem> _todoItems;
public TodoItemsService(DbSet<TodoItem> todoItems)
{
_todoItems = todoItems;
}
List<TodoItem>? ITodoItemsService.GetAbunch(string[] myAnimals)
{
var animals = _todoItems.Where(i => myAnimals.Contains(i.Name))
.ToList();
return animals;
}
}
GitHub code of the API application.
Screenshot of the error in Visual Studio:
I study how to write API in .NET 8. I created data layer as a service, but when I try to add service with AddScoped
I'm getting runtime exception. How to solve it?
System.AggregateException: 'Some services are not able to be constructed
(Error while validating the service descriptor 'ServiceType: TodoApiDb.Data.ITodoItemsService Lifetime:
Scoped ImplementationType: TodoApiDb.Data.TodoItemsService':
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet`1[TodoApiDb.Models.TodoItem]'
while attempting to activate 'TodoApiDb.Data.TodoItemsService'.)'
Program.cs
:
builder.Services.AddScoped<ITodoItemsService, TodoItemsService>();
ITodoItemsService.cs
:
public interface ITodoItemsService
{
List<TodoItem>? GetAbunch(string[] myAnimals);
}
TodoItemsService.cs
:
public class TodoItemsService: ITodoItemsService
{
private readonly DbSet<TodoItem> _todoItems;
public TodoItemsService(DbSet<TodoItem> todoItems)
{
_todoItems = todoItems;
}
List<TodoItem>? ITodoItemsService.GetAbunch(string[] myAnimals)
{
var animals = _todoItems.Where(i => myAnimals.Contains(i.Name))
.ToList();
return animals;
}
}
GitHub code of the API application.
Screenshot of the error in Visual Studio:
Share Improve this question edited Apr 3 at 6:37 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 30 at 2:15 sam sergiy kloksam sergiy klok 64413 silver badges27 bronze badges 1 |2 Answers
Reset to default 1You should get the TodoContext
instead of DbSet<TodoItem>
from the DI container.
public class TodoItemsService: ITodoItemsService
{
private readonly TodoContext _context;
public TodoItemsService(TodoContext context)
{
_context = context;
}
List<TodoItem>? ITodoItemsService.GetAbunch(string[] myAnimals)
{
var animals = _context.TodoItems.Where(i => myAnimals.Contains(i.Name))
.ToList();
return animals;
}
}
DbSet is an object managed inside DbContext and cannot be injected directly
DbSet<TodoItem>
? – mjwills Commented Mar 30 at 4:10