Export (0) Print
Expand All

How to: Send Simple Messages 

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:

  • Create a connection to the queue to which you want to send a message.

  • Specify the appropriate formatter for the data you want to send. The formatter controls what type of data can be sent in a message and how that data is persisted. In simple cases, the default formatter is acceptable. For more information, see Message Serialization.

  • Call the Send method, passing the object to be sent.

Creating a Connection to the Queue

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 either from the Toolbox, from Server Explorer to your project, or by using the MessageQueue constructor.

To create a connection to the queue you want to communicate with

  1. Create an instance of the MessageQueue component. For more information, see How to: Create MessageQueue Component Instances.

  2. Use the component's Path property to connect to the queue with which you want to communicate by the queue path, format name, or label.

    NoteNote

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

Providing the Data to Be Sent

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.

NoteNote

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 How to: Send Complex Messages.

To send a simple message

  • In the Properties window for your MessageQueue component, accept the default value for the Formatter, or set the property to XmlMessageFormatter if you have changed the default value.

    Use the Send method to send a simple message to your queue, specifying the message as an argument of the method.

    // Create a connection to the queue.   System.Messaging.MessageQueue mq =      new System.Messaging.MessageQueue(@".\YourQueue");   // Send an integer.   mq.Send(1);   // Send a string.   mq.Send("Hello world");
    
    // Create a connection to the queue.
    System.Messaging.MessageQueue mq =  new 
    System.Messaging.MessageQueue(".\\YourQueue");
    // Send an integer.
    mq.Send(new java.lang.Integer(1));
    // Send a string.
    mq.Send("Hello world");
    

    This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Operating System > Message Queues. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

See Also

Community Additions

ADD
Show:
© 2014 Microsoft