Home > OS >  How to resolve Microsoft.Extensions.Hosting services error?
How to resolve Microsoft.Extensions.Hosting services error?

Time:01-17

I try to add migration in my EF Core 5 project. All entities are set up well. Unfortunately when I do add-migration InitialMigration then the following error occurs:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MediatR.IRequestHandler`2[TicketManagement.Application.Features.Events.Commands.CreateEvent.CreateEventCommand,System.Guid] Lifetime: Transient ImplementationType: TicketManagement.Application.Features.Events.Commands.CreateEvent.CreateEventCommandHandler': Unable to resolve service for type 'TicketManagement.Application.Models.Mail.EmailSettings' while attempting to activate 'TicketManagement.Infrastructure.Mail.EmailService'.) (Error while validating the service descriptor 'ServiceType: TicketManagement.Application.Contracts.Infrastructure.IEmailService Lifetime: Transient ImplementationType: TicketManagement.Infrastructure.Mail.EmailService': Unable to resolve service for type 'TicketManagement.Application.Models.Mail.EmailSettings' while attempting to activate 'TicketManagement.Infrastructure.Mail.EmailService'.)

I added all services to Startup.cs class.

public void ConfigureServices(IServiceCollection services)
  {
     services.AddApplicationServices();
     services.AddPersistenceServices(Configuration);
     services.AddInfrastructureServices(Configuration);
     services.AddControllers();

     services.AddSwaggerGen(c =>
         {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "TicketManagement.Api", Version = "v1" });
      });

     services.AddCors(options =>
          {
             options.AddPolicy("Open", builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
          });
}

WHat's the root cause of this kind of error?

CodePudding user response:

You need to add EmailSettings to the service collection as EmailService does not know how to resolve the service.

  • Related