CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

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


Published Sep 02 2004, 01:00 PM by Brendan Tompkins
Filed under:

Comments

Jay Nathan said:

# October 4, 2004 4:22 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Free Tech Publications