Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
WSMQ - Creating Queues, Queuing and Receiving Messages

For an introduction to WSMQ, see my blog category here.

To use WSMQ from a .NET app, you'll need to install WSE 2.0, as WSMQ uses WSE Authentication for security.  Get a copy from Microsoft here.

So, how do you create a Queue using WSMQ?   The easiest way is through code.

Add a reference to the WSMQ Service (right click References ... Add web reference) in your VS project, then add the following using statements to your class:

using Microsoft.Web.Services2.Security;
using Microsoft.Web.Services2.Security.Tokens;
using localhost; // Or whatever gets you to the service

Next, create a secure request.  If you're going to be calling this multiple times, you may want to wrap this code in a helper method which returns the reference to the service proxy. If you've properly installed WSE, you should see the MessageQueueServiceWse proxy.  The code to create the request looks like this:

MessageQueueServiceWse mqs = new MessageQueueServiceWse();

// Create the token
UsernameToken token = new UsernameToken("wsuser", "password", PasswordOption.SendNone);

// Add the signature element to a security section on the request
// to sign the request
mqs.RequestSoapContext.Security.Tokens.Add( token );
mqs.RequestSoapContext.Security.Elements.Add(
new MessageSignature( token ) );

// Set the TTL to one minute to prevent reply attacks
mqs.RequestSoapContext.Security.Timestamp.TtlInSeconds = 60;

Next, create a CreateQueueRequest to create the queue for the first time, your app should only do this of course if it can't open the existing queue using an OpenQueueRequest();

CreateQueueRequest cqr = new CreateQueueRequest();
cqr.HasDeadLetter =
true;
cqr.HasJournal =
true;
cqr.IsPrivate =
false;
cqr.QueueName = “My First WSMQ Queue“;
mqs.CreateMessageQueue(cqr);

Finally, create a SendQueueRequest to actually send your message:

SendQueueRequest qr = new SendQueueRequest();
qr.QueueName =
“My First WSMQ Queue“;
qr.Message = “Hello“;
mqs.Send(qr);

Now, if you check the Queue XML file in your Queues directory, you should see your message.  In this example the message is a simple string, but you can also queue any XML serializable object this way.  For example using a serialization helper like this one here, you can serialize an object before adding the serialized XML to the message like so.

qr.Message = SerializationHelper.XmlSerialize(new ComplexClass()).OuterXml;

Finally, to receive your message, just do this:

QueueRequest qr = new QueueRequest();
qr.QueueName = “My First WSMQ Queue“;

string s = mqs.Receive(qr);

That should get you started, there's more to it, like using asynchronous sending and receiving, but those are the basics...

-Brendan


Posted 09-02-2004 1:00 PM by Brendan Tompkins
Filed under:

[Advertisement]

Comments

Jay Nathan wrote CodeSmith, WSMQ and Other .NET Goodness
on 10-04-2004 4:22 AM

Add a Comment

(required)  
(optional)
(required)  
Remember Me?