简体   繁体   English

在 Saga 中发布消息,其中类型仅在运行时已知

[英]Publish message in Saga where type is known only runtime

I want to publish a message from a Saga where the messgae type is not known compile time, only runtime.我想从 Saga 发布一条消息,其中 messgae 类型未知编译时间,只有运行时。 So far i've only found methods for publishing a message with generic signature - i cannot find any where i can publish a runtime type.到目前为止,我只找到了发布带有通用签名的消息的方法——我找不到任何可以发布运行时类型的地方。

I'm in the works of modifying the Sample-Batch application https://github.com/MassTransit/Sample-Batch to be able to batch any message type我正在修改 Sample-Batch 应用程序https://github.com/MassTransit/Sample-Batch以便能够批处理任何消息类型

How Sample-Batch is configured Sample-Batch 的配置方式

Initially(
    When(BatchJobReceived)
        .Then(context => Touch(context.Saga, context.Message.Timestamp))
        .Then(context => SetReceiveTimestamp(context.Saga, context.Message.Timestamp))
        .Then(Initialize)
        .PublishAsync(context => context.Init<ProcessBatchJob>(new
        {
            BatchJobId = context.Saga.CorrelationId,
            InVar.Timestamp,
            context.Saga.BatchId,
            context.Saga.OrderId,
            context.Saga.Action
        }))
        .TransitionTo(Received));

What i want to accomplish我想完成什么

  1. Receive the BatchJobReceived message接收BatchJobReceived消息
  2. Deserialize the inner message Json using InnerMessageAsString and InnerMessageType使用InnerMessageAsStringInnerMessageType
  3. Store the inner message on the state将内部消息存储在 state 上
  4. Publish the inner message And then keep state of the messages when they return发布内部消息,然后在消息返回时保留消息的 state
Initially(
    When(BatchJobReceived)
        .Then(context => Touch(context.Saga, context.Message.Timestamp))
        .Then(context => SetReceiveTimestamp(context.Saga, context.Message.Timestamp))
        .Then(Initialize)
        .Then(context => DeserializeInnerMessage(context.Saga, context.Message))
        .PublishAsync(context => context.Init(context.Saga.InnerMessage, context.Saga.InnerMessageType))
        .TransitionTo(Received));
static void DeserializeInnerMessage(BatchJobState state, BatchJobReceived job)
{
    var innerMessageType = Type.GetType(job.InnerMessageType);
    var innerMessage = JsonConvert.DeserializeObject(job.InnerMessageAsString, innerMessageType);

    state.InnerMessage = innerMessage;
}

There are however no context.Init() methods taking a Type parameter, it solely have a generic signature context.Init<>() .然而,没有context.Init()方法采用 Type 参数,它只有一个通用签名context.Init<>()

How do i accomplish this?我如何做到这一点? Or any other approaches more suitable?或者其他更合适的方法?

There isn't one, you can use your own ThenAsync statement to publish an unknown type as object :没有,您可以使用自己的ThenAsync语句将未知类型发布为object

.ThenAsync(x => x.Publish(x.Saga.InnerMessage, x.Saga.InnerMessageType)

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

相关问题 将一种类型的通用列表转换为另一种类型,其中仅在运行时知道这些类型 - Transforming a generic list of one type to another type, where the types are only known at runtime 将FormCollection转换为仅在运行时已知的类型的对象 - Convert FormCollection to object of type known only at runtime 动态对象强制转换为仅在运行时已知的类型 - Dynamic object cast to type known at runtime only 动态调度按类型仅在运行时已知 - Dynamic dispatch by Type only known at runtime 如何将ProjectTo从基类型转换为仅在运行时已知的类型? - How to ProjectTo from a base type to a type only known at runtime? 我可以实现泛型类型并将其传递给仅在运行时才知道的类型吗? - Can I implement a generic type and pass it a type that is known only at runtime? 将对象转换为仅在运行时已知的类型变量 - Casting object to type into variable of said type known only at runtime 使用Moq设置类型通用的方法,直到运行时才知道类型? - Using Moq to setup generic methods where the type is not known until runtime? 想要使功能 <UnknownType, bool> 其中UnknownType仅在运行时已知 - Want to make Func<UnknownType, bool> where UnknownType is only known at runtime 返回谓词&lt;T&gt;的方法(其中T仅在运行时已知) - Method that returns a Predicate < T > (where T is only known at runtime)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM