[英]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我想完成什么
BatchJobReceived
messageBatchJobReceived
消息InnerMessageAsString
and InnerMessageType
InnerMessageAsString
和InnerMessageType
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.