How Do I...Receive a message from a message queue?
Message queuing makes it easy for application developers to communicate with application
programs quickly and reliably by sending and receiving messages. Messaging provides you
with guaranteed message delivery and a robust, fail-safe way to carry out many of your
business processes.
The MessageQueue component allows you to easily incorporate message-based communication into your applications.
Using this component and its associated language features, you can send and receive messages, explore existing
queues, create and delete queues, and perform a variety of other operations using a simple programming model.
The sample illustrates how to use the MessageQueue component to receive a message from a message queue. To run
the sample you have to have Message Queuing installed on your system. The sample is a command-line application
that takes one command-line argument. The argument is the name of a public message queue on your local machine.
For example, you can run it as follows (first send a message to the queue using the MQSend sample):
> MQReceive.exe MyQueue
The sample application will receive and output to the console the first message from the queue.
In its simplest form, receiving a message from a message queue involves:
- Creating an instance of the MessageQueue component, setting its Path, and initializing the Formatter's
TargetTypeNames property:
MessageQueue mq = new MessageQueue(".\\MyQueue");
string[] types = { "System.String" };
((XmlMessageFormatter)mq.Formatter).TargetTypeNames = types;
C#
|
- Calling Receive to receive the message:
Message m = mq.Receive(new TimeSpan(0,0,3));
Console.WriteLine((string)m.Body);
C#
|
Example
C# MQReceive.exe
[This sample can be found at M:\web\users\sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Services\MessageQueue\MQReceive\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|