Код: Выделить всё
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Business.Abstract.ICustomerService Lifetime: Scoped ImplementationType: Business.Concrete.CustomerManager': Unable to resolve service for type 'DataAccess.Concrete.EntityFramework.SqlContext' while attempting to activate 'DataAccess.Concrete.EntityFramework.EfCustomerDal'.) (Error while validating the service descriptor 'ServiceType: DataAccess.Abstract.ICustomerDal Lifetime: Scoped ImplementationType: DataAccess.Concrete.EntityFramework.EfCustomerDal': Unable to resolve service for type 'DataAccess.Concrete.EntityFramework.SqlContext' while attempting to activate 'DataAccess.Concrete.EntityFramework.EfCustomerDal'.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: KodiksProject.API.Services.EmailBackgroundService': Unable to resolve service for type 'Business.Concrete.CustomerManager' while attempting to activate 'KodiksProject.API.Services.EmailBackgroundService'.)'
Как устранить эту проблему?
ICustomerService
Код: Выделить всё
public interface ICustomerService
{
IResult Add(Customer customer);
IResult Update(Customer customer);
IResult Delete(int id);
IDataResult Get(int id);
IDataResult GetAll();
Task SendEmailsToCustomersAsync();
}
Код: Выделить всё
public class CustomerManager : ICustomerService
{
ICustomerDal _customerDal;
private readonly IEmailService _emailService;
public CustomerManager(ICustomerDal customerDal, IEmailService emailService) {
_customerDal = customerDal;
_emailService = emailService;
}
public IResult Add(Customer customer)
{
_customerDal.Add(customer);
return new SuccessResult();
}
public IResult Delete(int id)
{
_customerDal.Delete(id);
return new SuccessResult();
}
public IDataResult Get(int id)
{
return new SuccessDataResult(_customerDal.Get(c => c.CustomerId == id));
}
public IDataResult GetAll()
{
return new SuccessDataResult(_customerDal.GetAll());
}
public IResult Update(Customer customer)
{
_customerDal.Update(customer);
return new SuccessResult();
}
public async Task SendEmailsToCustomersAsync()
{
var customerEmails = await _customerDal.GetAllCustomerEmailsAsync();
var tasks = new List();
foreach (var email in customerEmails)
{
var task = _emailService.SendEmailAsync(email, "asdasas", "asasadasdasd");
tasks.Add(task);
}
await Task.WhenAll(tasks);
}
}
Код: Выделить всё
public class SqlContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"my connection string");
}
public DbSet Customers { get; set; }
public DbSet Orders { get; set; }
public DbSet
Products { get; set; }
}
Код: Выделить всё
public class EfCustomerDal : EfEntityRepositoryBase, ICustomerDal
{
private readonly SqlContext _context;
public EfCustomerDal(SqlContext context)
{
_context = context;
}
public async Task GetAllCustomerEmailsAsync()
{
var emails = await _context.Customers
.Select(c => c.Email)
.ToListAsync();
return emails;
}
}
Код: Выделить всё
public interface ICustomerDal : IEntityRepository
{
Task GetAllCustomerEmailsAsync();
}
Код: Выделить всё
public class EmailService : IEmailService
{
private readonly SmtpClient _smtpClient;
public EmailService()
{
_smtpClient = new SmtpClient("server")
{
Port = 465,
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true,
};
}
public async Task SendEmailAsync(string to, string subject, string body)
{
var mailMessage = new MailMessage("username", to, subject, body);
await _smtpClient.SendMailAsync(mailMessage);
}
}
Я застрял здесь и не знаю, что делать.
Можете ли вы мне помочь в этой ситуации?
Подробнее здесь: https://stackoverflow.com/questions/786 ... ersion-8-0