Search code examples
c#asp.net-coreservice-worker

ASP.NET Core Windows Worker Service - Error while validating the service descriptor


I have this code in ASP.NET Core 6 Windows worker service that reads from (consumes) the queue and also write to another queue.

However, I got this error:

System.AggregateException
  HResult=0x80131500
  Message=Some services are not able to be constructed (Error while validating the service descriptor 
'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: 
NIPIncomingStatusVerification_Queue.Worker': Unable to resolve service for type 
'Implementations.RabbitMQManager' 
while attempting to activate 'Implementations.NIPIncomingStatusVerificationQueueService'.) 
(Error while validating the service descriptor 'Interfaces.IRabbitMQManager 
Lifetime: Singleton ImplementationType: .Implementations.RabbitMQManager': 
Unable to resolve service for type 'RabbitMQManager' while attempting to activate 
'VerificationQueueService'.) 
(Error while validating the service descriptor 'ServiceType: IQueueService Lifetime: 
Singleton ImplementationType: Implementations.QueueService': 
Unable to resolve service for type 'Implementations.RabbitMQManager' while attempting to activate 
'Implementations.QueueService'.)
  Source=Microsoft.Extensions.DependencyInjection
  StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at Program.<Main>d__1.MoveNext() in C:\NIPIncomingStatusVerification-Queue\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: Microsoft.Extensions.Hosting.IHostedService Lifetime: 
Singleton ImplementationType: NIPIncomingStatusVerification_Queue.Worker': Unable to resolve service for type 
'Implementations.RabbitMQManager' while attempting to activate 'Implementations.NIPIncomingStatusVerificationQueueService'.

Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'Implementations.RabbitMQManager' while attempting to activate 
'Implementations.QueueService'.

Kindly help resolve it.

NOTE:

  1. It consumes a queue and processes it
  2. It writes to another queue

appsettings.json:


Solution

  • The issue is in the NIPIncomingStatusVerificationQueueService, You need to inject the interface of RabbitMQManager like this:

    public class NIPIncomingStatusVerificationQueueService : INIPIncomingStatusVerificationQueueService
    {
        ...
        private readonly IRabbitMQManager _rabbitMqManager; // before you were inject RabbitMQManager which is an implementation of IRabbitMQManager
    
        public NIPIncomingStatusVerificationQueueService(
            INIPIncomingStatusVerificationQueueRepository creditRequestRepository,
            IConfiguration configuration,
            ApplicationSettings appSettings,
            ILogger<NIPIncomingStatusVerificationQueueService> logger,
            IRabbitMQManager rabbitMqManager
        )
        {
            ...
            _rabbitMqManager = rabbitMqManager;
    
        }
    ...
    

    The service resolve will done by DI at runtime and it will inject the implementation of your interface.