find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

Some services are not able to be constructed-ASP.NET CORE


I am working on worker service .NET 5 Core and  I am getting the following error:

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Provider.Setup.IInstrumentProvider Lifetime: Scoped ImplementationType: Provider.Setup.InstrumentProvider': Unable to resolve service for type 'Repository.Setup.InstrumentRepository' while attempting to activate 'Provider.Setup.InstrumentProvider'.)

This is my Repository:

   public class InstrumentRepository : Repository<Instrument>, IInstrumentRepository
    {         public InstrumentRepository(DataContext context) : base(context)
        {
        }
    }
   public interface IInstrumentRepository : IAsyncRepository<Instrument>, IRepository<Instrument>
    {
    }

This is my DependencyInjector class:

 public static class DependencyInjector
    {
        public static IServiceCollection RegisterServiceDependencies(this IServiceCollection services, int i = 0)
        {
            //repository        
            services.AddScoped<IInstrumentRepository, InstrumentRepository>(); 
            return services;         }     }

In my Startup.cs:

      public void ConfigureServices(IServiceCollection services)
        {             services.AddControllersWithViews();
            services.RegisterServiceDependencies();
            services.AddDataStore(Configuration);
    }


This is my service provider

namespace Provider.Setup
{
    public interface IInstrumentsProvider
    {      
        int Delete(int id);
    }
    public class InstrumentsProvider : IInstrumentsProvider
    {
        private readonly InstrumentRepository _instrumentRepository;
        public InstrumentsProvider(InstrumentRepository instrumentRepository)
        {
            _instrumentRepository = instrumentRepository;
        }    
       
        public int Delete(int id)
        {
            var data = _instrumentRepository.GetSingle(x => x.InstrumentId == id);
            if (data != null)
            {
                data.DeletedDate = DateTime.Now;
                _instrumentRepository.Update(data);
            }
            return 1;
        }      
    }
}


Answer


Option 1.

In this case you must call interface class not class like as given below.

        private readonly InstrumentRepository _instrumentRepository;
        public InstrumentsProvider(InstrumentRepository instrumentRepository)
        {             _instrumentRepository = instrumentRepository;
        }

    To

        private readonly IInstrumentRepository _instrumentRepository;
      //InstrumentRepository  this class replace by interface class IInstrumentRepository 
        public InstrumentsProvider(IInstrumentRepository instrumentRepository)
        {
            _instrumentRepository = instrumentRepository;
        }

 Hope this is working fine.


Option 2.

In the InstrumentRepository  constructor, you will see that you are injecting an instance of a InstrumentRepository  class. ASP.NET doesn't know where to search for an instance of that class to inject, so you have to register it in your DI container.

Add this code in your ConfigureServices

services.AddTransient<InstrumentRepository  >(new InstrumentRepository  ());


Option 3.

Essentially, you inject an IServiceScopeFactory into your singleton service. When you need a scoped service, you create a scope and resolve the service from that:

public class DemoSingletonService
{
    private IServiceScopeFactory Services { get; }
    public DemoSingletonService(IServiceScopeFactory services)
    {
        Services = services;
    }
    public void DoSomethingWithScopedService()
    {
        using (var scope = Services.CreateScope())
        {
            var myScopedService = scope.GetRequiredService<IScopedService>();
          //use here           
        }
    }
}

"The Microsoft example injects IServiceProvider, but IServiceScopeFactory is more appropriate, since you only need the CreateScope method."


Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

Comments



Report Response