Home > Back-end >  Some services are not able to be constructed IHostedService
Some services are not able to be constructed IHostedService

Time:05-10

I am trying to consume azure service bus by using .net core 6 web api. Whenever I try to run the api but It says:

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Azure.Messaging.ServiceBus.ServiceBusClient Lifetime: Singleton ImplementationType: Azure.Messaging.ServiceBus.ServiceBusClient': No constructor for type 'Azure.Messaging.ServiceBus.ServiceBusClient' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: Azure.Messaging.ServiceBus.ServiceBusProcessor Lifetime: Singleton ImplementationType: Azure.Messaging.ServiceBus.ServiceBusProcessor': A suitable constructor for type 'Azure.Messaging.ServiceBus.ServiceBusProcessor' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: DocumentiveEmailAPI.Consumers.CompleteRegistrationConsumer': No constructor for type 'Azure.Messaging.ServiceBus.ServiceBusClient' can be instantiated using services from the service container and default values.)'

Startup.cs

builder.Services.AddSingleton<ServiceBusClient>();
builder.Services.AddSingleton<ServiceBusProcessor>();
builder.Services.AddHostedService<CompleteRegistrationConsumer>();

CompleteRegistrationConsumer.cs

public class CompleteRegistrationConsumer : IHostedService
{
    private readonly ServiceBusConfiguration _configuration;
    private readonly ServiceBusClient _client;
    private readonly ServiceBusProcessor _processor;
    private readonly IEmailService _emailService;

    public CompleteRegistrationConsumer(IOptions<ServiceBusConfiguration> configuration, ServiceBusClient client, ServiceBusProcessor processor, IEmailService emailService)
    {
        _configuration = configuration.Value;
        _client = new ServiceBusClient(_configuration.ConnectionString);
        _processor = _client.CreateProcessor("CompleteRegistrationEmailQueue", new ServiceBusProcessorOptions());
        _emailService = emailService;

    }

    private async Task Processor_ProcessMessageAsync(ProcessMessageEventArgs arg)
    {
       var messageJson = JsonConvert.DeserializeObject<CompleteRegistrationEvent>(arg.Message.Body.ToString());

        var completeRegistrationRequest = new CompleteRegistrationEvent();
        completeRegistrationRequest.EmailAddress = messageJson.EmailAddress;
        completeRegistrationRequest.VerificationCode = messageJson.VerificationCode;


        await _emailService.SendCompleteRegistrationEmail(completeRegistrationRequest);
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        _processor.ProcessMessageAsync  = Processor_ProcessMessageAsync;
        await _processor.StartProcessingAsync();
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        _processor.DisposeAsync();
        _client.DisposeAsync();
        return Task.CompletedTask;
    }

}

It is not staring ServiceBusClient. I couldn't find any solution and article that can help me.

CodePudding user response:

As the error states, it cannot find a proper constructor for ServiceBusClient. Therefore when adding it you need to provide a delegate constructing it, such as: Services.AddSingleton(services => new ServiceBusClient("connectionString", null)).

CodePudding user response:

Your code is redundant. You want to create Singletons and use them with DI but you are still creating new ServiceBusClient and Processor inside the constructor of CompleteRegistrationConsumer. Only do one of these. (probably only inside CompleteRegistrationConsumer).

So remove the two singleton creations and you are good to go.

  • Related