こんにちは、エンジニアはMassTransitの次の問題を解決する方法を知っています。コンシューマはリクエストと応答を取得しますが、レスポンスはclient.Requestに戻りません。方法。私は、ASP NET WEB APIでプロジェクトを作成していると私はIRequestClientインタフェースによって要求/応答通信を実装している:私はAutofacにモジュールとしてserviceBusの構成を作成しているMassTransitはコンシューマからの応答を受け取ります
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
}
}
:
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の内のキューにすべてのメッセージを確認し、この1のようになります。 RabbitMQ queue
私はすでにクリス・パターソンによって作られたサンプル・RequestResponseを見てきましたが、ときに私、私は問題を抱えています依存性注入を使用する。 私は私が間違ってやっていることの助けのために感謝するでしょう..あなたは、このコードが含まれている単純なプロジェクトとまだdoesntの仕事を見つけることができますGitHubの上のすべてのリポジトリもあります:My GitHub