Asp.net MassTransit获得消费者的响应

Asp.net MassTransit获得消费者的响应,asp.net,asp.net-web-api,rabbitmq,masstransit,consumer,Asp.net,Asp.net Web Api,Rabbitmq,Masstransit,Consumer,嗨,enybody是否知道如何解决MassTransit中的以下问题:消费者获得请求和响应,但响应不会返回到client.request。方法。我在ASP NET WEB API中创建了项目,并通过IRequestClient接口实现了请求/响应通信: public class RequestResponseCommandProvider<TRequest, TResponse> : IRequestResponseCommandProvider<TRequest, T

嗨,enybody是否知道如何解决MassTransit中的以下问题:消费者获得请求和响应,但响应不会返回到client.request。方法。我在ASP NET WEB API中创建了项目,并通过IRequestClient接口实现了请求/响应通信:

public class RequestResponseCommandProvider<TRequest, TResponse>
    : IRequestResponseCommandProvider<TRequest, TResponse>
    where TRequest : class, ICommandQueueName
    where TResponse : class
{
    private readonly IBusControl _bus;
    private readonly string _hostUri;
    public RequestResponseCommandProvider(IBusControl bus,
        string hostUri)
    {
        _bus = bus;
        _hostUri = hostUri;
    }

    public TResponse RequestResponseCommand(TRequest command)
    {
        _bus.Start();
        var serviceAddress = new Uri(_hostUri + command.QueueName);
        IRequestClient<TRequest, TResponse> client =
            _bus.CreateRequestClient<TRequest, TResponse>(serviceAddress, TimeSpan.FromSeconds(10));
        return client.Request(command).Result; //there should back response
    }
}
公共类RequestResponseCommandProvider
:IRequestResponseCommandProvider
其中TRequest:class,ICommandQueueName
在哪里响应:类
{
专用只读IBusControl总线;
私有只读字符串_hostUri;
公共请求响应命令提供程序(IBusControl总线,
字符串(hostUri)
{
_总线=总线;
_hostUri=hostUri;
}
公共treresponse RequestResponseCommand(TRequest命令)
{
_bus.Start();
var serviceAddress=新Uri(_hostUri+command.QueueName);
IRequestClient客户端=
_CreateRequestClient(serviceAddress,TimeSpan.FromSeconds(10));
返回client.Request(command.Result;//应该有响应返回
}
}
我已在Autofac中创建serviceBus作为模块的配置:

public class BusModule : Autofac.Module
{
    private readonly string _hostUri;
    IEnumerable<IConfigurableConsumer> _consumers;

    public BusModule(string hostUri, IEnumerable<IConfigurableConsumer> consumers)
    {
        _hostUri = hostUri;
        _consumers = consumers;
    }

    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies());

        builder.Register(r => Bus.Factory.CreateUsingRabbitMq(sfc =>
        {
            var host = sfc.Host(new Uri(_hostUri), h =>
            {
                h.Username("guest");
                h.Password("guest");
            });

            if (_consumers != null)
            {
                foreach (var consumer in _consumers)
                {
                    consumer.Configure(sfc);
                }
            }
        }))
        .As<IBus>()
        .As<IBusControl>()
        .SingleInstance();

        builder.RegisterType<RecieveObserver>()
            .As<IReceiveObserver>();
    }
}
公共类总线模块:自动传真模块
{
私有只读字符串_hostUri;
i可数消费者;
公共总线模块(字符串hostUri,IEnumerable使用者)
{
_hostUri=hostUri;
_消费者=消费者;
}
受保护的覆盖无效负载(ContainerBuilder builder)
{
RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblyTypes());
builder.Register(r=>Bus.Factory.CreateUsingRabbitMq(sfc=>
{
var host=sfc.host(新Uri(_hostUri),h=>
{
h、 用户名(“客人”);
h、 密码(“客人”);
});
如果(_consumers!=null)
{
foreach(var消费者在_消费者中)
{
消费者配置(sfc);
}
}
}))
.As()
.As()
.SingleInstance();
builder.RegisterType()
.As();
}
}
使用者由构造函数添加。 将提供程序注入服务:

public class TestLayer : ITestLayer
{
    private readonly IRequestResponseCommandProvider<IAddTestCommand, ResponseCommand> _provider;
    public TestLayer(
        IRequestResponseCommandProvider<IAddTestCommand, ResponseCommand> provider)
    {
        _provider = provider;
    }
    public ServiceResult CreateTest(TestRecord record)
    {
        ServiceResult result;
        try
        {
            var tmp = _provider.RequestResponseCommand(new AddTestCommand() { Record = "d3d32" });
            result = new ServiceResult();
        }
        catch (Exception ex)
        {
            result = new ServiceResult();
            result.AddError($"Wystąpił problem podczas zapisu do bazy danych: {ex}");
        }

        return result;
    }
}
公共类测试层:ITestLayer
{
专用只读IRequestResponseCommandProvider\u provider;
公共测试层(
IRequestResponseCommandProvider(供应商)
{
_提供者=提供者;
}
公共服务结果CreateTest(TestRecord记录)
{
服务结果;
尝试
{
var tmp=_provider.RequestResponseCommand(new AddTestCommand(){Record=“d3d32”});
结果=新服务结果();
}
捕获(例外情况除外)
{
结果=新服务结果();
结果.加法器($“Wystąpił问题podczas zapisu do bazy danych:{ex}”);
}
返回结果;
}
}
当我检查RabbitMQ中的队列时,所有消息都如下所示:

我已经看到了Chris Patterson所做的示例RequestResponse,但我在使用依赖注入时遇到了问题。 我将感激你对我所做错事的帮助。。GitHub上还有所有存储库,您可以在其中找到包含此代码但仍然无法工作的简单项目:

两个问题:

  • 总线的延迟实例化不是一个好主意,因为它需要相当长的时间,在您的情况下,当
    IBus
    第一次被解析时,您将得到一个很长的超时
  • 您没有收到任何回复,因为您需要启动总线,它才能接收任何信息。如果你不启动公共汽车,你只能发送

  • 事实上,我们需要启动总线才能收到响应