Deserializing messages directly

522 views
Skip to first unread message

Clay Anderson

unread,
Apr 19, 2012, 6:00:24 PM4/19/12
to masstrans...@googlegroups.com
I'm looking to build a UI to query, modify and/or retry failed messages. I'd love to show the message type name and a summary of the message utilizing ToString() on the deserialized message. I'm using RabbitMQ.

I can get the raw RabbitMQ messages body via IModel.BasicGet, but I can't get the type:
    BasicGetResult getResult = //call to RabbitMQ
    XDocument xml = XDocument.Parse(Encoding.UTF8.GetString(getResult.Body));
    Type messageType = new MessageUrn(xml.Root.Element("messageType").Value).GetType();
    // fails because my namespace doesn't match the assembly name

Can I use MassTransit to deserialize the bytes into an object if I don't have a Bus receiving from the error queue and I don't know the exact message type at compile time?

Thanks.

Dru Sellers

unread,
Apr 19, 2012, 6:05:21 PM4/19/12
to masstrans...@googlegroups.com
Are you using the XmlSerializer? or the Json or the Binary?

You can try not doing the GetType() and just use the MessageUrn - if its a UI you should just need the string. Eh?

-d

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/ETPD5es88AYJ.
To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-dis...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/masstransit-discuss?hl=en.

Chris Patterson

unread,
Apr 19, 2012, 7:27:08 PM4/19/12
to masstrans...@googlegroups.com
The code on how MassTransit resolves messageType to type is in the deserializer code. And it does include the namespace, so you should be able to find it.

Clay Anderson

unread,
Apr 20, 2012, 10:21:15 AM4/20/12
to masstrans...@googlegroups.com
Thanks for the fast responses! I'm using XML serialization.

Using the Message URN directly allows me to show the message type. I just strip out the "urn:message:" part.

But I'd still like to deserialize and call ToString on the deserialized object to get a summary of the message. This works, however, it requires that I know the type at compile time (and it may not be the simplest way to do this).

BasicGetResult getResult = //get from RabbitMQ
MemoryStream bodyStream = new MemoryStream(getResult.Body);
ReceiveContext receive = ReceiveContext.FromBodyStream(bodyStream);
XmlMessageSerializer serializer = new XmlMessageSerializer();
serializer.Deserialize(receive);
IConsumeContext<MyMessageClass> consume;
receive.TryGetContext(out consume);
MyMessageClass message = consume.Message;

I saw some information about using Magnum's FastInvoke to invoke a generic method with a type determined at run-time. However, I'm not able to get the type at run-time as the message urn doesn't contain the assembly name, only the namespace.
    BasicGetResult getResult = //call to RabbitMQ
    XDocument xml = XDocument.Parse(Encoding.UTF8.GetString(getResult.Body));
    Type messageType = new MessageUrn(xml.Root.Element("messageType").Value).GetType();
    // fails because my namespace doesn't match the assembly name

I can have an assembly per namespace, which should allow MessageUrn.GetType() to work. Or, I can use reflection to scan all the loaded types to look for the match. Is there another way?

Thanks.

Clay

Dru Sellers

unread,
Apr 20, 2012, 10:37:01 AM4/20/12
to masstrans...@googlegroups.com
Yeah - that's probably not going to work, because you would have to have EVERY message type available. Better off (probably) just showing the XML form. :(

-d

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.

Clay Anderson

unread,
May 1, 2012, 9:43:40 AM5/1/12
to masstrans...@googlegroups.com
Thanks Dru,

We went with just showing the message type (instead of ToString) in our summary page. It doesn't need to be too fancy as we're hoping that the error queue doesn't need managed much.

Clay
-d


To post to this group, send email to masstransit-discuss@googlegroups.com.
To unsubscribe from this group, send email to masstransit-discuss+unsub...@googlegroups.com.

Dru Sellers

unread,
May 1, 2012, 9:46:01 AM5/1/12
to masstrans...@googlegroups.com
:)

I look forward to hearing about how it eventually get's used in anger. ;)

-d

To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/0Gi1V5PskP4J.

To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-dis...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages