Dependency Injection - Error: InvalidOperationException: Unable to resolve service for type

1

Friends, once more I come to you for help.

When trying to call an Action from my Controller I'm getting the problem described below:

  

An unhandled exception occurred while processing the request.   InvalidOperationException: Unable to resolve service for type   'Muo.Application.Interfaces.ICompanyService' while attempting to   activate 'Muo.Presentation.Web.Controllers.CompanyController'.       Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService (IServiceProvider   sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

What seems to be the problem is in Dependency Injection. However, I'm finding it strange, as I'm doing this with AutoMapper. Here are my classes:

In Application Layer - Part of AutoMapper

public class DomainToViewModelMappingProfile : Profile
{
    public DomainToViewModelMappingProfile()
    {
         //Transformo minha entidade numa ViewModel
         CreateMap<Company, CompanyViewModel>();
    }
}


public class ViewModelToDomainMappingProfile : Profile
{
    public ViewModelToDomainMappingProfile()
    {
         //Transformo minha ViewModel em Entidade de Domínio
         CreateMap<CompanyViewModel, RegisterNewCompanyCommand>()
                .ConstructUsing(c => new RegisterNewCompanyCommand(c.OficialName, c.Nickname, c.Cnpj, c.Type, c.StateRegistration, c.DistrictRegistration));
         CreateMap<CompanyViewModel, UpdateCompanyCommand>()
                .ConstructUsing(c => new UpdateCompanyCommand(c.Id, c.OficialName, c.Nickname, c.Cnpj, c.Type, c.StateRegistration, c.DistrictRegistration));       
    }
}


public static class AutoMapperSetup
{
    public static void AddAutoMapperSetup(this IServiceCollection services)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));

        services.AddAutoMapper();

        AutoMapperConfig.RegisterMappings();
    }
}

In the Presentation Layer, in the Startup.cs class of the ASP.NET Core project, register the AutoMapper service as below:

public void ConfigureServices(IServiceCollection services)
{
   //...

   services.AddAutoMapperSetup();
}

What could be happening?

Please help me!

    
asked by anonymous 07.10.2018 / 17:00

1 answer

2

You have been missing the ICompanyService interface. For further questions, please see documentation

public void ConfigureServices(IServiceCollection services)
{
   services.AddScoped<ICompanyService, CompanyService>();
   services.AddAutoMapperSetup();
}

The first generic type represents the type (usually an interface) that will be requested from the container. The second generic type represents the concrete type that will be instantiated by the container and used to meet those requests.

ASP.NET services can be configured with the following useful lifetimes:

Transient

Transient objects are always different; a new instance is provided for all controllers and all services.

Scoped

Objects with Scoped are the same in one request, but different between different requests

Singleton

singleton objects are the same for each object and each request (regardless of an instance being supplied ConfigureServices )

Source

    
09.10.2018 / 15:38