Question

Im wondering what happens when a message is received through an MS message queue and the messagebody is cast to the actual object.

System.Messaging.Message msg = queue.Receive();

Order order = (Order)msg.Body;

The body contains some XML but it has type object. So does the object get serialized with the XmlSerializer at the line of the cast or has that already happened?

Or more general, does serialization always happen when casting object to the actual type or is there no extra code generated?

Was it helpful?

Solution

Looking at the System.Messaging.Message.Body property you can see its of type Object.

What that means is the the messaging framework of MSMQ takes care of the serialization/deserialization for you.

The documentation states that any object passed through the Body property must be serializable:

The Body property can be any serializable object, such as a text string, structure object, class instance, or embedded object.

There for sure is no serialization going on when you cast to your type of object.

OTHER TIPS

I'm wondering what happens when a message is received through an MS message queue and the messagebody is cast to the actual object

It depends on what the type of msg.Body really is.

If msg.Body is a string of XML, then casting to Order will fail at runtime.

If msg.Body is an Order object that was transmitted as XML and has been deserialized, then your cast will succeed.

does serialization always happen when casting object to the actual type or is there no extra code generated?

There's no "magic" that deserializes an XML string when you cast - either it's an Order or it's not. If it's an XML string, it must be explicitly deserialized into a different type.

Unless something has changed in Message Queue over the years, you cannot simply cast as a type, as there is no magic to convert the XML to the type you desire. If there is some newer magic I am unaware of, the object would be serialized underneath the hood and the plumbing is added to MSMQ or the abstraction for .NET (or C#) to do it. You can debug through the .NET bits now, if you are really interested, by adding the Microsoft repositories.

In general, serialization is not automatic. You can cast from a general type to a specific type only when there is a proper cast available. Otherwise the cast fails. WCF has some magic in it, but it is largely in the generated proxy, with a bit in the internals (if you prefer, use plumbing instead of the word magic).

Also, in general, it is a bad practice to rely on magic. In most cases, you should explicitly state the serializer when you pull from a queue and then use it to regenerate the objects. I allow some forgiveness for WCF. Please note that this is a more general statement, as relying on default behavior, especially when it is not documented, can lead to future disasters.

Casting happens in the class itself. This is not the exact code but this is what happens when do casting. Assume that Body is a object type:

    public static implicit operator Body( Order d )
    {
        return new Order(d);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top