Message Headers

Every WCF message contains a collection of outgoing and incoming message headers. When the client wishes to send out-of-band parameters to the service, it does so by adding those parameters to the outgoing headers. The service then reads those parameters from the incoming headers.

The operation context offers collections of incoming and outgoing headers, available via the IncomingMessageHeaders and OutgoingMessageHeaders properties:

public sealed class OperationContext : ...
{
   public MessageHeaders IncomingMessageHeaders
   {get;}

   public MessageHeaders OutgoingMessageHeaders
   {get;}

   //More members
}

Each collection is of the type MessageHeaders (that is, a collection of MessageHeader objects):

public sealed class MessageHeaders : ...
{
   public void Add(MessageHeader header);
   public T GetHeader<T>(int index);
   public T GetHeader<T>(string name,string ns);
   //More members
}

The class MessageHeader is not intended for application developers to interact with directly. Instead, use the MessageHeader<T> class, which provides for type-safe and easy conversion from a CLR type parameter to a message header:

public abstract class MessageHeader : ...
{...}

public class MessageHeader<T>
{
   public MessageHeader(  );
   public MessageHeader(T content);
   public T Content
   {get;set;}
   public MessageHeader GetUntypedHeader(string name,string ns);
   //More members
}

You can use any serializable or data contract type as the type parameter for MessageHeader<T>. You construct a MessageHeader<T> around a CLR ...

Get Programming WCF Services, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.