Skip to main content
3 de 3
se añadieron 6 caracteres en el cuerpo
fredyfx
  • 13.2k
  • 11
  • 33
  • 65

Inyección de dependencias c# error

Tengo un problema al intentar hacer una inyección de dependencias en C# cuando ejecuto me da el siguiente error. Startup

System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: EstiloPlusWeb.Helpers.IConverterHelper Lifetime: Scoped ImplementationType: EstiloPlusWeb.Helpers.ConverterHelper': Unable to resolve service for type 'EstiloPlusWeb.Helpers.CombosHelper' while attempting to activate 'EstiloPlusWeb.Helpers.ConverterHelper'.) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at EstiloPlusWeb.Program.Main(String[] args) in E:\Proyectos\EstiloPlus\EstiloPlus\EstiloPlusWeb\Program.cs:line 16

This exception was originally thrown at this call stack: [External Code]

Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: EstiloPlusWeb.Helpers.IConverterHelper Lifetime: Scoped ImplementationType: EstiloPlusWeb.Helpers.ConverterHelper': Unable to resolve service for type 'EstiloPlusWeb.Helpers.CombosHelper' while attempting to activate 'EstiloPlusWeb.Helpers.ConverterHelper'.

Inner Exception 2: InvalidOperationException: Unable to resolve service for type 'EstiloPlusWeb.Helpers.CombosHelper' while attempting to activate 'EstiloPlusWeb.Helpers.ConverterHelper'.

Tengo las siguientes clases, interfas, startup

startup

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddScoped<ICombosHelper, CombosHelper>();
        services.AddControllersWithViews();
        services.AddScoped<IConverterHelper, ConverterHelper>();

    }

ConverterHelper

    namespace EstiloPlusWeb.Helpers
{
    public class ConverterHelper : IConverterHelper
    {
        private readonly ApplicationDbContext _context;
        private readonly CombosHelper _combosHelper;

        public ConverterHelper(ApplicationDbContext context, CombosHelper  combosHelper)
        {
            _context = context;
            _combosHelper = combosHelper;
        }


        public EmpleadoViewModel ToEmpleadoViewModel(Empleado empleado)
        {
            return new EmpleadoViewModel
            {
                Nombre = empleado.Nombre,
                Apellido = empleado.Apellido,
                Cedula = empleado.Cedula,
                Direccion = empleado.Direccion,
                Celular = empleado.Celular,
                Email = empleado.Email,
                FechaIngreso = empleado.FechaIngreso,
                Categoria = empleado.Categoria,
                CategoriaId = empleado.Categoria.Id,
                Categorias = _combosHelper.GetComboCategorias()
            };
        }
    }
}

IConverterHelper

    namespace EstiloPlusWeb.Helpers
{
    public interface IConverterHelper
    {
        Models.ViewsModels.EmpleadoViewModel ToEmpleadoViewModel(Empleado empleado);
    }
}

comboHelper

   namespace EstiloPlusWeb.Helpers
{
    public class CombosHelper : ICombosHelper
    {
        private readonly ApplicationDbContext _context;
        public CombosHelper(ApplicationDbContext context)
        {
            _context = context;
        }

        public IEnumerable<SelectListItem> GetComboCategorias()
        {
            var list = _context.Categoria.Select(pt => new SelectListItem
            {
                Text = pt.Descripcion,
                Value = $"{pt.Id}"
            })
                .OrderBy(pt => pt.Text)
                .ToList();
            list.Insert(0, new SelectListItem
            {
                Text = "[Seleccione la categoría..]",
                Value = "0"
            });
            return list;
        }
    }
}

IComboHelper

namespace EstiloPlusWeb.Helpers
{
    public interface ICombosHelper
    {
        IEnumerable<SelectListItem> GetComboCategorias();
    }
}

La verdad que no estoy entendiendo cúal es el motivo por el que me lanza ese error. Pueden ayudarme ? Muchas gracias