c# – InvalidOperationException: Unable to resolve service for type ** mi proyecto **

I am creating a simple example web api but I get an error from

InvalidOperationException: Unable to resolve service for type ** mi proyecto **

in my startup class I reviewed it with addScope

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped();
            services.AddControllersWithViews();
            services.AddSession();
            //services.AddTransient();
            services.AddControllers();
            services.AddMemoryCache();
        }

in my controller I already did everything I put it in the constructor of the class like this

    [Route("api/[controller]")]
    [ApiController]
    public class ProductoIntelectualController : ControllerBase
    {
        private readonly IProductosService productosService;

        public ProductoIntelectualController(IProductosService productosService)
        {
            this.productosService = productosService;
        }

----demas codigo

in the applicationDbContext I have calling the connection string to the configuration json

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            
            if (!optionsBuilder.IsConfigured)
            {
                IConfigurationRoot configuration = new ConfigurationBuilder()
                    .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                    .AddJsonFile("appsettings.json")
                    .Build();
                optionsBuilder.UseSqlServer(configuration.GetConnectionString("ApplicactionDbContext"));
            }
        }

when I leave the AddScope I get the error:

Mensaje = Some services are not able to be constructed (Error while
validating the service descriptor ‘ServiceType:
xxxxxDomain.IProductosService Lifetime: Scoped ImplementationType:
xxxxxx.Domain.ProductosService’: Unable to resolve service for type
‘xxxxx.Datos.Repositories.Interfaces.IProductoRepository’ while
attempting to activate ‘xxxxx.Domain.ProductosService’.) Origen =
Microsoft.Extensions.DependencyInjection Seguimiento de la pila:
at
Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, IServiceProviderEngine engine, 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
Ips.DocInv.WebApi.Program.Main(String[] args) in

InvalidOperationException: Error while validating the service
descriptor ‘ServiceType: xxxx.Domain.IProductosService Lifetime:
Scoped ImplementationType: xxxx.Domain.ProductosService’: Unable to
resolve service for type
‘xxxx.Datos.Repositories.Interfaces.IProductoRepository’ while
attempting to activate ‘xxxx.Domain.ProductosService’.

Excepci├│n interna 2: InvalidOperationException: Unable to resolve
service for type
‘xxxxxx.Datos.Repositories.Interfaces.IProductoRepository’ while
attempting to activate ‘xxxxx.Domain.ProductosService’.

if I remove it I place it in a comment (services.AddScoped();) I get the error mentioned initially.

I don’t know what I’m skipping I have already done all the instructions in on here

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.