admin管理员组

文章数量:1356011

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
  • 4 Show us your registration for DbSet<TodoItem>? – mjwills Commented Mar 30 at 4:10
Add a comment  | 

2 Answers 2

Reset to default 1

You 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

本文标签: