Исключение, которое я получаю;
System.AggregateException: 'Некоторые службы невозможно создать (ошибка при проверке дескриптора службы 'ServiceType: SchoolManagementApp.Core.UnitOfWorks.IUnitOfWork Срок службы: Scoped ImplementationType: SchoolManagementApp.Dal.UnitOfWorks.UnitOfWork': невозможно разрешить службу для типа) «SchoolManagementApp.Dal.AppDbContext» при попытке активировать «SchoolManagementApp.Dal.UnitOfWorks.UnitOfWork».) (Ошибка при проверке дескриптора службы «ServiceType: SchoolManagementApp.Core.Repositories.IStudentRepository Срок службы: Scoped ImplementationType: SchoolManagementApp.Dal.Repositories. StudentRepository»: невозможно разрешить службу для типа «SchoolManagementApp.Dal.AppDbContext» при попытке активировать «SchoolManagementApp.Dal.Repositories.StudentRepository».) (Ошибка при проверке дескриптора службы «ServiceType: SchoolManagementApp.Core.Services.IStudentService Lifetime: Scoped ImplementationType: SchoolManagementApp.Service.Services.StudentService»: невозможно разрешить службу для типа «SchoolManagementApp.Dal.AppDbContext» при попытке активировать «SchoolManagementApp.Dal.Repositories.GenericRepository`1[SchoolManagementApp.Core.Models.Student]». )'
program.cs
Код: Выделить всё
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped(typeof(IGenericRepository), typeof(GenericRepository));
builder.Services.AddScoped(typeof(IService), typeof(Service));
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Код: Выделить всё
namespace SchoolManagementApp.Dal
{
public class AppDbContext : DbContext
{
public AppDbContext()
{
}
public DbSet Students { get; set; }
public DbSet Lessons { get; set; }
public DbSet Lecturers { get; set; }
public DbSet LessonOfStudents { get; set; }
public DbSet ActiveLessons { get; set; }
public DbSet CollegeDepartments { get; set; }
public DbSet CollegeDepartmentsActiveLessons { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new LessonConfiguration());
modelBuilder.ApplyConfiguration(new StudentConfiguration());
modelBuilder.ApplyConfiguration(new LecturerConfiguration());
modelBuilder.ApplyConfiguration(new LessonOfStudentConfiguration());
modelBuilder.ApplyConfiguration(new ActiveLessonConfiguration());
modelBuilder.ApplyConfiguration(new CollegeDepartmentConfiguration());
modelBuilder.ApplyConfiguration(new CollegeDepartmentsActiveLessonConfiguration());
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//Todo Configuration read
optionsBuilder.UseSqlServer("Data Source=DESKTOP-H2SJGIR;Initial Catalog=DbSchoolManagement;Integrated Security=True");
}
}
}
Код: Выделить всё
namespace SchoolManagementApp.Dal.UnitOfWorks
{
public class UnitOfWork : IUnitOfWork
{
private readonly AppDbContext _context;
public UnitOfWork(AppDbContext context)
{
_context = context;
}
public void Commit()
{
_context.SaveChanges();
}
public async Task CommitAsync()
{
await _context.SaveChangesAsync();
}
}
}
Код: Выделить всё
namespace SchoolManagementApp.Core.UnitOfWorks
{
public interface IUnitOfWork
{
Task CommitAsync();
void Commit();
}
}
Код: Выделить всё
namespace SchoolManagementApp.Core.Repositories
{
public interface IStudentRepository : IGenericRepository
{
}
}
Код: Выделить всё
namespace SchoolManagementApp.Dal.Repositories
{
public class StudentRepository : GenericRepository,IStudentRepository
{
public StudentRepository(AppDbContext context) : base(context)
{
}
}
}
Код: Выделить всё
namespace SchoolManagementApp.Service.Services
{
public class Service : IService where T : class
{
private readonly IGenericRepository _repository;
private readonly IUnitOfWork _unitOfWork;
public Service(IGenericRepository genericRepository, IUnitOfWork unitOfWork)
{
_repository = genericRepository;
_unitOfWork = unitOfWork;
}
//Service methods
Код: Выделить всё
namespace SchoolManagementApp.Core.Services
{
public interface IStudentService : IService { }
}
Код: Выделить всё
namespace SchoolManagementApp.Service.Services
{
public class StudentService : Service, IStudentService
{
private readonly IStudentRepository _studentRepository;
private readonly IUnitOfWork _unitOfWork;
public StudentService(IGenericRepository genericRepository, IUnitOfWork unitOfWork, IStudentRepository studentRepository) : base(genericRepository, unitOfWork)
{
_studentRepository = studentRepository;
_unitOfWork = unitOfWork;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/761 ... cted-error