MassTransit得到消费者的回应

时间:2016-09-18 13:19:24

标签: 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, 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
    }
}

我在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>();
    }
}

构造函数添加了消费者。 提供商注入服务:

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

当我在RabbitMQ中检查队列时,所有消息都是这样的: RabbitMQ queue

我已经看过Chris Patterson制作的Sample-RequestResponse,但是当我使用依赖注入时我遇到了问题。 我将非常感谢帮助我所做的错误.GitHub上还有所有存储库,您可以在其中找到包含此代码但仍无效的简单项目:My GitHub

1 个答案:

答案 0 :(得分:1)

两个问题:

  1. 总线的懒惰实例化不是一个好主意,因为它需要相当长的时间,在你的情况下,当IBus第一次解决时,你会得到很长的超时。
  2. 您没有收到任何回复,因为您需要启动公交车才能接收任何内容。如果您没有启动公交车,则只能发送。