Sending 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 will 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.
For more information on the properties you can use to fine-tune your Message object, see Message Members.
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
- 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 Creating 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.
- Create an instance of the Message object.
- Set the body of the message and specify any properties you want to change from their default values.
- Use the Send method to send your object to the queue.
When you are finished, your code might look like this:
' Visual Basic Dim myMQ1 As New System.Messaging.MessageQueue(".\YourQueue") Dim newMessage As New System.Messaging.Message("Hello again") newMessage.Label = "This is the label" myMQ1.Send(newMessage) // C# 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);