[英]Transforming a generic list of one type to another type, where the types are only known at runtime
[英]Publish message in Saga where type is known only runtime
我想从 Saga 发布一条消息,其中 messgae 类型未知编译时间,只有运行时。 到目前为止,我只找到了发布带有通用签名的消息的方法——我找不到任何可以发布运行时类型的地方。
我正在修改 Sample-Batch 应用程序https://github.com/MassTransit/Sample-Batch以便能够批处理任何消息类型
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));
我想完成什么
BatchJobReceived
消息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;
}
然而,没有context.Init()
方法采用 Type 参数,它只有一个通用签名context.Init<>()
。
我如何做到这一点? 或者其他更合适的方法?
没有,您可以使用自己的ThenAsync
语句将未知类型发布为object
:
.ThenAsync(x => x.Publish(x.Saga.InnerMessage, x.Saga.InnerMessageType)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.