有些服务无法构建 IHostedService

Some services are not able to be constructed IHostedService

我正在尝试使用 .net core 6 web api 使用 azure 服务总线。每当我尝试 运行 api 但它说:

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;
    }

}

它没有启动 ServiceBusClient。我找不到任何可以帮助我的解决方案和文章。

如错误所述,它无法为 ServiceBusClient 找到合适的构造函数。因此在添加的时候需要提供一个构造它的委托,比如:Services.AddSingleton(services => new ServiceBusClient("connectionString", null)).

您的代码是多余的。你想创建单例并将它们与 DI 一起使用,但你仍在 CompleteRegistrationConsumer 的构造函数中创建新的 ServiceBusClient 和处理器。只做其中一项。 (可能只在 CompleteRegistrationConsumer 内)。

所以删除两个单例创建,你就可以开始了。