在MassTransit使用者中获取动态对象 [英] Obtain dynamic object in MassTransit consumer

查看:29
本文介绍了在MassTransit使用者中获取动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为需要将事件保存到某些审核日志数据库中的一组事件使用一个空标记界面.

We use an empty marker interface for a group of events that need to be saved to some audit log database.

但是,在使用者中,消息被强制转换为该接口,所以我们得到一个空对象.

However, in the consumer, messages are cast to this interface, so we get an empty object.

我们真正需要的是获取动态"消息或保留消息正文,因此我们可以按原样将其发送到审核数据库,因为我们的数据库可以保存JSON文档.但是我们看不到如何从上下文中将消息正文作为JSON获取.可能吗?

What we actually need is to get the "dynamic" or get hold on the message body so we can send it to the audit database "as-is" since our database can save JSON documents. But we cannot see how we can get the message body as JSON from the context. Is it possible at all?

推荐答案

如果您真的想提高效率,可以让消费者使用今天的接口,然后在您的消费者中,从消息上下文,并使用JToken保存消息的JSON.这样,您的使用者就不需要知道每个单一的对象类型,也不需要该对象类型的程序集.

If you really wanted to be efficient, you could keep your consumer using the interface as it is today, but then, in your consumer, get the JToken from the message context, and use the JToken to save the JSON of the message. This way, your consumer doesn't need to know every single object type nor have the assembly for that object type.

public async Task Consume(ConsumeContext<IEvent> context)
{
    ConsumeContext<JToken> jsonContext;
    if(context.TryGetMessage(out jsonContext))
    {
        _eventStore.Save(jsonContext.Message); // the JToken
    }
}

这篇关于在MassTransit使用者中获取动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆