繁体   English   中英

如何在 Asp.Net Core 应用程序中配置 MassTransit Saga

[英]How to configure MassTransit Saga in Asp.Net Core application

我正在尝试将简单的 MassTransit Saga 集成到 ASP.NET 核心应用程序中。 ConfigureServices期间,我有:

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

后来我发布消息:

var bus = context.RequestServices.GetService<IBusControl>();
await bus.Publish<IRequestCreated>(new
{
    CorrelationId = Guid.NewGuid(),
    ClientId = 1,
});

但它永远不会到达 Saga 实例。

我的传奇是这样的:

    public class RequestStateMachine : MassTransitStateMachine<Request>
    {
        public RequestStateMachine()
        {
            InstanceState(x => x.CurrentState);

            Event(
                () => RequestCreated,
                x => x.CorrelateById(context => context.Message.CorrelationId).SelectId(context => Guid.NewGuid()));

            Initially(
                When(RequestCreated)
                    .Then(context =>
                    {
                        Console.WriteLine($"Request received, id = {context.Instance.CorrelationId}");
                        context.Instance.RequestId = 10;
                    })
                    .TransitionTo(Active)
            );

            SetCompletedWhenFinalized();
        }

        public State Active { get; protected set; }

        public Event<IRequestCreated> RequestCreated { get; protected set; }
    }

    public class Request : SagaStateMachineInstance
    {
        public string CurrentState { get; set; }

        public Guid CorrelationId { get; set; }

        public long RequestId { get; set; }

        public Guid? ExpirationId { get; set; }
    }

我想我做错了什么,但不知道是什么。

我不得不承认这有点令人困惑。 我们在 Microsoft DI package 和 ASP.NET 核心集成 package 中都有AddMassTransit方法,它们做不同的事情。

AspNetCoreIntegration AddMassTransit中的 AddMassTransit 还注册了启动和停止总线的服务。 因此,此代码将解决您的问题:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ISagaRepository<Request>, InMemorySagaRepository<Request>>();
    services.AddMassTransit(
        provider => 
            Bus.Factory.CreateUsingInMemory(cfg =>
            {
                cfg.UseInMemoryOutbox();
                cfg.ConfigureEndpoints(provider);
            },
        x => x.AddSagaStateMachine<RequestStateMachine, Request>()
    );
}

您使用的方法只是将总线注册为容器中的IBusIBusControlISendEndpointProviderIPublishEndpointPervider ,但它并不关心启动和停止总线。 我在代码示例中使用的方法还注册了主机服务并(可选)添加了健康检查。

暂无
暂无

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

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