简体   繁体   中英

Send message to MassTransit Saga in ASP.Net Core application

I have a simple saga configuration in Asp.Net Core applicaiton:

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> :

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 :

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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