我想在Azure函数项目中使用rabbitmq

8ulbf1ek  于 2022-12-04  发布在  RabbitMQ
关注(0)|答案(1)|浏览(142)

我想在Azure函数项目中使用rabbitmq
我放在startup.cs中的代码如下所示

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddHttpClient();
        builder.Services.AddApplication();
        builder.Services.AddInfrastructure();
        builder.Services.AddTransient<ReporterAuthenticationRequestHandler>();
        builder.Services.AddHttpClient<IReporterApiService, ReporterApiService>()
                        .AddHttpMessageHandler<ReporterAuthenticationRequestHandler>();

#region Cap
        builder.Services.AddCap(options =>
            {
                options.UseMongoDB("mongodb://localhost:27017");

                options.UseRabbitMQ(x =>
                {
                    x.HostName = "localhost";
                    x.UserName = "guest";
                    x.Password = "guest";
                    x.Port = 5672;
                });
            });

#endregion
    }
}

但我得到一个错误:
在启动操作'7 b42 aad 5-f38 c-4062-b255- 4ccaa 12754 ea'期间发生主机错误。[2022-12- 01 T06:57:44.400Z]函数:主机服务无效。Microsoft。Azure。WebJobs。脚本。WebHost:以下服务注册与预期服务不匹配:[2022-12- 01 T06:57:44.400Z] [无效]服务类型:Microsoft.扩展.托管.IHostedService,生存期:单例,实现工厂:System.Func2[系统.IServiceProvider,DotNetCore.CAP.内部.引导程序].值不能为空.(参数“provider”)
你知道我该怎么做吗?

evrscar2

evrscar21#

您看到的错误似乎是由于在代码中向Microsoft.Extensions.Hosting.IHostedService服务注册传递了一个空值。错误消息指示提供程序参数为空,这表明服务提供程序未正确初始化或未传递给AddCap()方法。
若要修正此错误,您可以尝试下列步骤:
请确保在项目中安装了Microsoft.Azure.Functions.Extensions NuGet包。此包提供您要在Startup类中扩展的FunctionsStartup类,以及您要用于配置服务的IFunctionsHostBuilder接口。在配置()方法,使用builder.Services属性初始化服务提供程序并将其传递给AddCap()方法。这将确保正确初始化服务提供程序并将其传递给AddCap()方法。下面是如何执行此操作的示例:

public override void Configure(IFunctionsHostBuilder builder)
{
    // Initialize the service provider
    var services = builder.Services;

    // Add other services and dependencies as needed

    // Configure Cap
    services.AddCap(options =>
        {
            options.UseMongoDB("mongodb://localhost:27017");

            options.UseRabbitMQ(x =>
            {
                x.HostName = "localhost";
                x.UserName = "guest";
                x.Password = "guest";
                x.Port = 5672;
            });
        });
}

进行这些更改后,Startup类应该能够初始化服务提供程序并正确配置Cap以在Azure Functions项目中使用。然后,你可以使用Cap在Azure Functions中使用RabbitMQ发布和使用消息。

相关问题