【发布时间】:2016-09-18 13:19:24
【问题描述】:
您好,是否有人知道如何解决 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>();
}
}
消费者由构造函数添加。 Provider 被注入到服务中:
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
【问题讨论】:
标签: asp.net asp.net-web-api rabbitmq masstransit consumer