How to: Send Complex Messages

In addition to sending a simple message by specifying your message in the Send method's argument, you can gain more control over your messages by explicitly creating a Message object, rather than letting the shared Send method create and send one for you. By manipulating the properties of the resulting Message object, you can create more complex messages and exert more control over the way your messages are handled.

When you send a complex message, you must first create a connection to the queue with which you want to communicate, and then specify the data to send. However, as part of specifying the data to send, you create an instance of the Message class, set the properties you need, and fine-tune the delivery mechanisms before sending your message. As with simple messages, the system persists your message object and sends it to the queue you specified.

Note

Using the Message object, you can send messages as part of a transaction. For more information, see Transactional Message Processing.

To send a complex message using the Message class

  1. Create an instance of the MessageQueue component and set its Path property to the queue to which you want to refer. For more information, see How to: Create MessageQueue Component Instances.

    Note

    If you created your component from Server Explorer, the Path property is set automatically to the queue path for that queue.

  2. Create an instance of the Message object.

  3. Set the body of the message and specify any properties you want to change from their default values.

  4. Use the Send method to send your object to the queue.

    When you are finished, your code might look like this:

    Dim MessageQueue1 As New System.Messaging.MessageQueue(".\YourQueue")
    Dim newMessage As New System.Messaging.Message("Hello again")
    newMessage.Label = "This is the label."
    MessageQueue1.Send(newMessage)
    
           System.Messaging.MessageQueue myMQ1 =
               new System.Messaging.MessageQueue(@".\YourQueue");
            System.Messaging.Message newMessage =
               new System.Messaging.Message("Hello again");
            newMessage.Label = "This is the label";
            myMQ1.Send(newMessage);
    

See Also

Tasks

How to: Create MessageQueue Component Instances

How to: Send Simple Messages

How to: Verify Messages at Design Time

Concepts

Transactional Message Processing

Other Resources

Sending and Serializing Messages