gpt4 book ai didi

c# - 将依赖项注入(inject) IConsumer

转载 作者:太空宇宙 更新时间:2023-11-03 20:47:44 24 4
gpt4 key购买 nike

有没有办法注入(inject) IConsumer?

例如

public class UpdateCustomerConsumer : IConsumer<ITestMessage>
{
private IJimTest _jimTest;

public UpdateCustomerConsumer(IJimTest jimTest)
{
_jimTest = jimTest;
}

我已经使用 MassTransit.Extensions.DependencyInjection nuget 包设置了 Startup.ConfigureServices()。

        services.AddMassTransit(x =>
{
x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host(new Uri("rabbitmq://localhost:5672/"), h => { });

cfg.ReceiveEndpoint("test", ep =>
{
ep.Consumer<Consumers.UpdateCustomerConsumer>();
});

cfg.ConfigureEndpoints(provider);
}));
});

services.AddSingleton<IJimTest, JimTest>();

但是 _jimTest 最终为空。

最佳答案

根据 documentation ,您的配置应如下所示:

        services.AddMassTransit(x =>
{
x.AddConsumer<Consumers.UpdateCustomerConsumer>(); //added this line
x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host(new Uri("rabbitmq://localhost:5672/"), h => { });

cfg.ReceiveEndpoint("test", ep =>
{
ep.ConfigureConsumer<Consumers.UpdateCustomerConsumer>(provider); //changed
});

//if you use cfg.ReceiveEndpoint this one is not needed
//you can remove cfg.ReceiveEndpoint and use below instead
//cfg.ConfigureEndpoints(provider);
}));
});

services.AddSingleton<IJimTest, JimTest>();

关于c# - 将依赖项注入(inject) IConsumer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59165349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com