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 |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
本文标签:
版权声明:本文标题:c# - AddScoped cause System.AggregateException: 'Some services are not able to be constructed (Error while validating th 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743998475a2573422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
DbSet<TodoItem>
? – mjwills Commented Mar 30 at 4:10