简体   繁体   English

在ASP.Net Core应用程序中将消息发送到MassTransit Saga

[英]Send message to MassTransit Saga in ASP.Net Core application

I have a simple saga configuration in Asp.Net Core applicaiton: 我在Asp.Net Core应用程序中有一个简单的传奇配置:

services.AddSingleton<ISagaRepository<Request>, InMemorySagaRepository<Request>>();
services.AddMassTransit(x =>
{
    x.AddSagaStateMachine<RequestStateMachine, Request>();
    x.AddRequestClient<IRequestCreated>();
    x.AddBus(provider => Bus.Factory.CreateUsingInMemory(cfg =>
    {
        cfg.UseInMemoryOutbox();
        cfg.ConfigureEndpoints(provider);
    }));
});

If later I send message to Saga over IRequestClient<IRequestCreated> : 如果以后我通过IRequestClient<IRequestCreated>向Saga发送消息:

var client = context.RequestServices.GetService<IRequestClient<IRequestCreated>>();
var response = await client.GetResponse<RequestCreatedResponse>(new
{
    CorrelationId = Guid.NewGuid(),
    ClientId = 1,
});

all works fine. 一切正常。 But if I try same thing over IBus : 但是,如果我通过IBus尝试相同的IBus

var mtbus = context.RequestServices.GetService<IBus>();
await mtbus.Send<IRequestCreated>(new
{
    CorrelationId = Guid.NewGuid(),
    ClientId = 1,
});

I get error A convention for the message type Sample.AspNetCore.Host.Saga.IRequestCreated was not found 我收到错误A convention for the message type Sample.AspNetCore.Host.Saga.IRequestCreated was not found

What am I missing? 我想念什么?

In your example above, because no service address is specified for the request client, it is publishing the request. 在上面的示例中,由于未为请求客户端指定服务地址,因此它正在发布请求。 Which gets routed to the consumer. 哪个路由到消费者。

In your failing case, you're using Send, which without an address needs to know where to send the message. 在失败的情况下,您使用的是发送,没有地址的发送者需要知道将消息发送到哪里。 The only fallback is the convention, which isn't configured. 唯一的后退是没有配置的约定。 If you want the same behavior, and honestly, since you're publishing a RequestCreated event, you should call Publish as well. 老实说,如果您想要相同的行为,因为您要发布RequestCreated事件,则也应该调用Publish。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM