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

Asked

Viewed 1,998 times

2

Friends, once again I come to you for help.

When trying to call an Action from my Controller I am 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, because I’m doing this with Automapper. Below follows my classes:

In Application Layer - Automapper Part

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, I register the Automapper service as below:

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

   services.AddAutoMapperSetup();
}

What might be going on?

Help me please!

1 answer

3


Missed you register interface ICompanyService. For further questions 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 these requests.

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

Transient

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

Scoped

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

Singleton

Objects singleton are the same for each object and each request (regardless of whether an instance is provided ConfigureServices)

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.