Freigeben über


Nothing to See Here

What is the System.ServiceModel.Channels.NullMessage used for?

The Message class is a central abstraction for representing communication between two endpoints. There's often interesting information associated with a message, such as the message delivery properties, and that interesting information hangs off of the Message class. Therefore, if you don't have a Message, then you don't have access to any of this information.

The requirement to have a Message isn't often a problem. Even if you don't have any content, then that doesn't necessarily mean that you don't have a message. An empty SOAP message is still a message. An empty SOAP message has an envelope, headers, and even a body; it just doesn't have any content within the body tag. Empty SOAP messages occur quite often when you have a two-way operation but no application data to send in one of the directions. Despite being empty, an empty SOAP message could have any amount of content thanks to arbitrarily extensible storage locations such as headers. A secure, empty SOAP message could easily be many kilobytes in size.

On the other hand, sometimes you have even less than an empty message. For example, if you're not using SOAP, then an empty message may really mean 0 bytes of data. Or, a read operation might result in an indication that no more input is coming. There truly isn't anything to build a message out of in that case.

In the early design of WCF, anything that was less than an empty message was represented by a null object. However, that later became a problem as it was recognized that without a Message object, other code might not work. You might have some of that interesting information that hangs off of the Message class even though you don't have a message to convey. The first example of that was POX messaging where you might receive HTTP headers but have the content of the message be a 0 byte stream.

The NullMessage class was a way to distinguish something that was less than an empty message while still providing a storage location for interesting information associated with a message. Now, the null object is used when there is even less than a NullMessage available. For example, if a stream has no more input available, then that corresponds to a null object, while if a stream conveys a message but the length of that message is 0 bytes, then that corresponds to a NullMessage.