Export (0) Print
Expand All

MessageHeaders Class

Represents a collection of message headers for a message. This class cannot be inherited.

Namespace:  System.ServiceModel.Channels
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public ref class MessageHeaders sealed : IEnumerable<MessageHeaderInfo^>, 
	IEnumerable

A message header contains optional metadata that describe a message encapsulated by the Message class.

A Message can have zero or more headers that are used as an extension mechanism to pass information in messages that are application-specific. You can use Headers to add message headers to a message by calling the Add method.

Windows Communication Foundation (WCF) provides a number of predefined message headers:

Header Name

Description

To

Contains the role that the message is targeting.

Action

Provides a description of how the message should be processed.

FaultTo

Contains the address of the node to which faults should be sent.

From

Contains the address of the node that sent the message.

Request

Indicates whether the message is a request.

MessageID

Contains the unique ID of the message.

RelatesTo

Contains the IDs of messages that are related to this message.

ReplyTo

Contains the address of the node to which a reply should be sent for a request.

The following code example demonstrates a basic client using the channel factory to send a message and read the action header from the reply.

#using <System.Messaging.dll>
#using <System.dll>

using namespace System;
using namespace System::Messaging;

public ref class Order
{
private:
    int orderIdValue;
private:
    DateTime orderTimeValue;

public:
    property int OrderId
    {
        int get()
        {
            return orderIdValue;
        }
        void set(int value)
        {
            orderIdValue = value;
        }
    }
public:
    property DateTime OrderTime
    {
        DateTime get()
        {
            return orderTimeValue;
        }
        void set(DateTime value)
        {
            orderTimeValue = value;
        }
    }

public:
    Order()
    {
    }
};

// Creates a new queue. 
void CreateQueue(String^ queuePath, bool transactional)
{
    if(!MessageQueue::Exists(queuePath))
    {
        MessageQueue^ queue = MessageQueue::Create(queuePath, transactional);
        queue->Close();
    }
    else
    {
        Console::WriteLine("{0} already exists.", queuePath);
    }
}

// Sends an Order to a queue. 
void SendMessage()
{
    // Create a new order and set values.
    Order^ sentOrder = gcnew Order();
    sentOrder->OrderId = 3;
    sentOrder->OrderTime = DateTime::Now;

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\orderQueue");

    // Create the new order.
    Message^ orderMessage = gcnew Message(sentOrder);

    // Set the message's TimeToReachQueue property to five minutes.
    orderMessage->TimeToReachQueue = TimeSpan::FromMinutes(5.0);

    // Display the new value of the message's TimeToReachQueue property.
    Console::WriteLine("Message.TimeToReachQueue: {0}",
        orderMessage->TimeToReachQueue.ToString());

    // Set the message's TimeToBeReceived property to five minutes.
    orderMessage->TimeToBeReceived = TimeSpan::FromMinutes(5.0);

    // Display the new value of the message's TimeToBeReceived property.
    Console::WriteLine("Message.TimeToBeReceived: {0}",
        orderMessage->TimeToBeReceived.ToString());

    // Set the message's UseTracing property to true.
    orderMessage->UseTracing = true;

    // Display the new value of the message's UseTracing property.
    Console::WriteLine("Message.UseTracing: {0}", orderMessage->UseTracing);

    // Set the message's Label property.
    orderMessage->Label = "Order Message";

    // Display the new value of the message's Label property.
    Console::WriteLine("Message Label: {0}", orderMessage->Label);

    // Set the message's AttachSenderId property.
    orderMessage->AttachSenderId = true;

    // Display the new value of the message's AttachSenderId property.
    Console::WriteLine("Message.AttachSenderId: {0}",
        orderMessage->AttachSenderId);

    // Set the message's Recoverable property.
    orderMessage->Recoverable = true;

    // Display the new value of the message's Recoverable property.
    Console::WriteLine("Message.Recoverable: {0}",
        orderMessage->Recoverable);

    // Set the message's ResponseQueue property. 
    // (You must have a connection to a queue.)
    orderMessage->ResponseQueue = queue;

    // Display the new value of the message's ResponseQueue property.
    Console::WriteLine("Message.ResponseQueue.QueueName: {0}",
        orderMessage->ResponseQueue->QueueName);

    // Set the message's TimeToReachQueue property to 
    // Message.InfiniteTimeout.
    orderMessage->TimeToReachQueue = Message::InfiniteTimeout;

    // Display the new value of the message's TimeToReachQueue property.
    Console::WriteLine("Message.TimeToReachQueue: {0}",
        orderMessage->TimeToReachQueue.ToString());

    // Set the message's UseDeadLetterQueue property.
    orderMessage->UseDeadLetterQueue = true;

    // Display the new value of the message's UseDeadLetterQueue property.
    Console::WriteLine("Message.UseDeadLetterQueue: {0}",
        orderMessage->UseDeadLetterQueue);

    // Set the message's UseJournalQueue property.
    orderMessage->UseJournalQueue = true;

    // Display the new value of the message's UseJournalQueue property.
    Console::WriteLine("Message.UseJournalQueue: {0}",
        orderMessage->UseJournalQueue);

    // Set the message's UseEncryption property.
    orderMessage->UseEncryption = true;

    // Display the new value of the message's UseEncryption property.
    Console::WriteLine("Message.UseEncryption: {0}",
        orderMessage->UseEncryption);

    // Send the order to the queue.
    queue->Send(orderMessage);

    queue->Close();
}

// Receives a message containing an order. 
void ReceiveMessage()
{
    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\orderQueue");

    // Set the formatter to indicate the message body contains an order.
    queue->Formatter = gcnew XmlMessageFormatter(gcnew array<Type^>
    {Order::typeid});

    // Set the queue's MessageReadPropertyFilter properties to ensure that 
    // the message includes the properties in which we are interested.
    queue->MessageReadPropertyFilter->ArrivedTime = true;
    queue->MessageReadPropertyFilter->DestinationQueue = true;
    queue->MessageReadPropertyFilter->SenderVersion = true;
    queue->MessageReadPropertyFilter->SentTime = true;
    queue->MessageReadPropertyFilter->SourceMachine = true;
    queue->MessageReadPropertyFilter->Authenticated = true;

    // Receive the message. Time out after ten seconds in case the message 
    // was not delivered.
    Message^ orderMessage = queue->Receive(TimeSpan::FromSeconds(10.0));

    // Format the message.
    Order^ sentOrder = (Order^)orderMessage->Body;

    // Display message information.
    Console::WriteLine("Order ID: {0}", sentOrder->OrderId.ToString());
    Console::WriteLine("Sent: {0}", sentOrder->OrderTime.ToString());

    // Display the value of the message's MessageType property.
    Console::WriteLine("Message Type: {0}", orderMessage->MessageType);

    // Display the value of the message's SourceMachine property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.SourceMachine property must be set to 
    // true before the message is received.
    Console::WriteLine("Source Machine: {0}", orderMessage->SourceMachine);

    // Display the value of the message's DestinationQueue property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.DestinationQueue property must be set to 
    // true before the message is received.
    Console::WriteLine("Message.DestinationQueue.QueueName: {0}",
        orderMessage->DestinationQueue->QueueName);

    // Display the value of the message's SentTime property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.SentTime property must be set to 
    // true before the message is received.
    Console::WriteLine("Time sent from source queue: {0}",
        orderMessage->SentTime.ToString());

    // Display the value of the message's ArrivedTime property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.ArrivedTime property must be set to 
    // true before the message is received.
    Console::WriteLine("Time arrived in destination queue: {0}",
        orderMessage->ArrivedTime.ToString());

    // Display the value of the message's Authenticated property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.Authenticated property must be set to 
    // true before the message is received.
    Console::WriteLine("Authentication requested for this message: {0}",
        orderMessage->Authenticated.ToString());

    // Display the value of the message's SenderVersion property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.SenderVersion property must be set to 
    // true before the message is received.
    Console::WriteLine("Message.SenderVersion: {0}",
        orderMessage->SenderVersion);

    // Message Queuing sets this property when the message is sent. 
    // Display the value of the message's BodyType property.
    Console::WriteLine("Message.BodyType: {0}",
        orderMessage->BodyType.ToString());

    queue->Close();
}

// Sends an Order to a transactional queue. 
void SendMessageToTransQueue()
{
    // Create a new order and set values.
    Order^ sentOrder = gcnew Order();
    sentOrder->OrderId = 3;
    sentOrder->OrderTime = DateTime::Now;

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\orderTransQueue");

    // Create the new order.
    Message^ orderMessage = gcnew Message(sentOrder);

    // Create a message queuing transaction.
    MessageQueueTransaction^ transaction = gcnew MessageQueueTransaction();

    try
    {
        // Begin a transaction.
        transaction->Begin();

        // Send the order to the queue.
        queue->Send(orderMessage, transaction);

        // Commit the transaction.
        transaction->Commit();
    }
    catch (MessageQueueException^ ex)
    {
        // Abort the transaction.
        transaction->Abort();

        // Propagate the exception. 
        throw;
    }
    finally
    {
        // Dispose of the transaction object. 
        delete transaction;

        queue->Close();
    }
}

// Receives a message containing an order. 
void ReceiveMessageFromTransQueue()
{
    Message^ orderMessage;

    // Connect to the a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\orderTransQueue");

    // Set the formatter to indicate the message body contains an order.
    queue->Formatter = gcnew XmlMessageFormatter(gcnew array<Type^>
    {Order::typeid});

    // Set the queue's MessageReadPropertyFilter properties to ensure that 
    // the message includes the properties in which we are interested.
    queue->MessageReadPropertyFilter->TransactionId = true;
    queue->MessageReadPropertyFilter->TransactionStatusQueue = true;
    queue->MessageReadPropertyFilter->IsFirstInTransaction = true;
    queue->MessageReadPropertyFilter->IsLastInTransaction = true;

    // Create a message queuing transaction.
    MessageQueueTransaction^ transaction = gcnew MessageQueueTransaction();

    try
    {
        // Begin a transaction.
        transaction->Begin();

        // Receive the message. Time out after ten seconds in case the 
        // message was not delivered.
        orderMessage = queue->Receive(TimeSpan::FromSeconds(10.0),
            transaction);

        // Commit the transaction.
        transaction->Commit();
    }
    catch (MessageQueueException^ ex)
    {
        // Abort the transaction.
        transaction->Abort();

        // Propagate the exception. 
        throw;
    }
    finally
    {
        // Dispose of the transaction object. 
        delete transaction;

        queue->Close();
    }

    // Message Queuing sets this property when the message is sent. 
    // Display the value of the message's TransactionStatusQueue property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.TransactionStatusQueue property must be 
    // set to true before the message is received.
    Console::WriteLine("Message.TransactionStatusQueue.QueueName: {0}",
        orderMessage->TransactionStatusQueue->QueueName);

    // Display the value of the message's TransactionId property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.TransactionId property must be set to true 
    // before the message is received.
    Console::WriteLine("Message.TransactionId: {0}",
        orderMessage->TransactionId);

    // Display the value of the message's IsFirstInTransaction property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.IsFirstInTransaction property must be set 
    // to true before the message is received.
    Console::WriteLine("Message.IsFirstInTransaction: {0}",
        orderMessage->IsFirstInTransaction);

    // Display the value of the message's IsLastInTransaction property. 
    // To view this property value, the queue's 
    // MessageReadPropertyFilter.IsLastInTransaction property must be set 
    // to true before the message is received.
    Console::WriteLine("Message.IsLastInTransaction: {0}",
        orderMessage->IsLastInTransaction);

}

int main()
{
    try
    {
        // Create a non-transactional queue on the local computer. 
        // Note that the queue might not be immediately accessible, and 
        // therefore this example might throw an exception of type 
        // System.Messaging.MessageQueueException when trying to send a 
        // message to the newly created queue.
        CreateQueue(".\\orderQueue", false);

        // Send a message to a queue.
        SendMessage();

        // Receive a message from a queue.
        ReceiveMessage();

        // Create a transactional queue on the local computer.
        CreateQueue(".\\orderTransQueue", true);

        // Send a message to a transactional queue.
        SendMessageToTransQueue();

        // Receive a message from a transactional queue.
        ReceiveMessageFromTransQueue();
    }

    catch (InvalidOperationException^)
    {
        Console::WriteLine("Please install Message Queuing.");
    }

    catch (MessageQueueException^ ex)
    {
        // Write the exception information to the console.
        Console::WriteLine(ex);
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;

namespace ConsoleApplication1
{
    class client
    {


        static void RunClient()
        {
            //Step1: create a binding with just HTTP
            CustomBinding binding = new CustomBinding();
            binding.Elements.Add(new HttpTransportBindingElement());
            //Step2: use the binding to build the channel factory
            IChannelFactory<IRequestChannel> factory =
            binding.BuildChannelFactory<IRequestChannel>(
                             new BindingParameterCollection());
            //open the channel factory
            factory.Open();
            //Step3: use the channel factory to create a channel
            IRequestChannel channel = factory.CreateChannel(
               new EndpointAddress("http://localhost:8080/channelapp"));
            channel.Open();
            //Step4: create a message
            Message requestmessage = Message.CreateMessage(
                MessageVersion.Soap12WSAddressing10,
                "http://contoso.com/someaction",
                 "This is the body data");
            //send message
            Message replymessage = channel.Request(requestmessage);
            Console.WriteLine("Reply message received");
            Console.WriteLine("Reply action: {0}",
                                  replymessage.Headers.Action);
            string data = replymessage.GetBody<string>();
            Console.WriteLine("Reply content: {0}", data);
            //Step5: don't forget to close the message
            requestmessage.Close();
            replymessage.Close();
            //don't forget to close the channel
            channel.Close();
            //don't forget to close the factory
            factory.Close();
        }
        public static void Main()
        {
            Console.WriteLine("Press [ENTER] when service is ready");
            Console.ReadLine();
            RunClient();
            Console.WriteLine("Press [ENTER] to exit");
            Console.ReadLine();
        }
    }
}

System::Object
  System.ServiceModel.Channels::MessageHeaders

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

.NET Compact Framework

Supported in: 3.5

Community Additions

ADD
Show:
© 2014 Microsoft