Obtain dynamic object in MassTransit consumer

 ̄綄美尐妖づ 提交于 2019-12-24 00:44:44

问题


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.

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?


回答1:


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
    }
}


来源:https://stackoverflow.com/questions/35343583/obtain-dynamic-object-in-masstransit-consumer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!