简体   繁体   English

是否可以关联 MassTransit Saga 中不基于 ID 的消息?

[英]Is it possible to to correlate a message in MassTransit Saga that is not based on a ID?

Is it possible to match an incoming message with a saga that doesn't have a 1 to 1 correlation of Id or property?是否可以将传入消息与没有 1 对 1 关联的 Id 或属性的 saga 匹配? I'm looking at doing something like the following but CorrelateBy does not seem to hydrate the saga or trigger off any processing.我正在考虑做类似以下的事情,但CorrelateBy似乎并没有滋润传奇或触发任何处理。

    public interface SubmitOrder
    {
        Guid OrderId { get; }

        string[] ItemIds { get; }
    }

    public interface ItemStockExhausted
    {
        string Id { get; }
    }

    public class OrderState :
        SagaStateMachineInstance
    {
        public Guid CorrelationId { get; set; }

        public string[] ItemIds { get; set; }

        public int CurrentState { get; set; }
    }

    public class OrderStateMachine :
        MassTransitStateMachine<OrderState>
    {
        public OrderStateMachine()
        {
            InstanceState(x => x.CurrentState);
            Event(() => SubmitOrder, x
                => x.CorrelateById(context => context.Message.OrderId)
            );

            Event(() => ItemStockExhausted, x
                => x.CorrelateBy((state, context) => state.ItemIds.Contains(context.Message.Id))
            );

            Initially(
                When(SubmitOrder)
                    .Then(x => x.Instance.ItemIds = x.Data.ItemIds)
                    .TransitionTo(Submitted));

            During(Submitted,
                When(ItemStockExhausted)
                    .Then(x => { /* Do something */ })
                    );
        }

        public Event<SubmitOrder> SubmitOrder { get; private set; }

        public Event<ItemStockExhausted> ItemStockExhausted { get; private set; }

        public State Submitted { get; private set; }
    }

Not sure if it makes any difference but I'm using the MongoDB persistence.不确定它是否有任何区别,但我正在使用 MongoDB 持久性。

Correlation by the query is possible for Saga persistence providers that support queries.对于支持查询的 Saga 持久性提供程序,可以通过查询进行关联。 MongoDB persistence provider supports using queries but you need to remember that not LINQ queries translate to MongoDB queries properly. MongoDB 持久性提供程序支持使用查询,但您需要记住,不是 LINQ 查询可以正确转换为 MongoDB 查询。

In your case, I would advise enabling MongoDB query tracing, like it's suggested in this question: How do I log my queries in MongoDB C# Driver 2.0?在您的情况下,我建议启用 MongoDB 查询跟踪,就像在这个问题中建议的那样: How do I log my queries in MongoDB C# Driver 2.0?

Some persistence providers, specifically those that are using key-value databases like Redis, won't support the correlation by queries but they normally throw the "unsupported" exception as soon as you try using a query.一些持久化提供者,特别是那些使用像 Redis 这样的键值数据库的持久化提供者,不支持查询关联,但他们通常会在您尝试使用查询时立即抛出“不支持”异常。

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

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