Using an instance of the MessageQueue component, you can send simple messages to a queue in as little as two lines of code. When you send a simple message, you must perform the following actions:
After you decide which queue you want to communicate with, you need to create an instance of the MessageQueue component that references the queue you want to use. You can create this component by using the MessageQueue constructor.
To create a connection to the queue you want to communicate with
Note If you created your component from Server Explorer, the Path property is set automatically to the queue path for that queue.
Each MessageQueue component contains a series of default properties that are applied to all messages sent by that component, unless you specify otherwise in an individual message. In the simplest scenario, you can send a simple message to the queue using the default properties set for the component. For more information on these properties, see Default Message Properties.
You use the Send method to specify a message and send it to the queue. You can send objects, primitive data types, streams, and other kinds of data in a simple message.
The MessageQueue component takes the data you specify in the Send method's argument, persists it into a message, and sends the message to the specified queue.
Note You can also use the Message object to send far more complex messages to a queue. In addition, you can send messages as part of a transaction. For more information, see Sending Complex Messages.
To send a simple message
Your code to send a simple message might look like this:
[Visual Basic] 'Create a connection to the queue Dim MessageQueue1 as new MessageQueue ("YourMachine\YourQueue") 'Send an integer MessageQueue1.Send(1) 'Send a string MessageQueue1.Send("Hello world") 'Persist an object and send it to the queue MessageQueue1.Send(System.DateTime.Now) [C#] // Create a connection to the queue MessageQueue mq = new MessageQueue (@"YourMachine\YourQueue"); // Send an integer mq.Send (1); // Send a string mq.Send ("Hello world"); // Persist an object and send it to the queue mq.Send (System.DateTime.Now);
Sending and Serializing Messages | Creating MessageQueue Components | Sending Complex Messages | Sending Messages Asynchronously | Transactional Message Processing