15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1366 ❘ ChaPTer 46 messAGe QueuinG<br />

queue TyPe<br />

Public queue<br />

Private queue<br />

Journal queue<br />

Machine journal queue<br />

Machine dead-letter queue<br />

Machine transactional dead-letter queue<br />

xynTaX<br />

MachineName\QueueName<br />

MachineName\Private$\QueueName<br />

MachineName\QueueName\Journal$<br />

MachineName\Journal$<br />

MachineName\DeadLetter$<br />

MachineName\XactDeadLetter$<br />

When you use the pathname to open public queues, it is necessary to pass the machine name. If the machine<br />

name is not known, the format name can be used instead. The pathname for private queues can be used only<br />

on the local system. The format name must be used to access private queues remotely.<br />

format name<br />

Instead of the pathname, you can use the format name to open a queue. The format name is used for<br />

searching the queue in the Active Directory to get the host where the queue is located. In a disconnected<br />

environment where the queue cannot be reached at the time the message is sent, it is necessary to use the<br />

format name:<br />

MessageQueue queue = new MessageQueue(<br />

@"FormatName:PUBLIC=09816AFF-3608-4c5d-B892-69754BA151FF");<br />

The format name has some different uses. It can be used to open private queues <strong>and</strong> to specify a protocol<br />

that should be used:<br />

➤<br />

➤<br />

➤<br />

To access a private queue, the string that has to be passed to the constructor is FormatName:<br />

PRIVATE=MachineGUID\QueueNumber. The queue number for private queues is generated when the<br />

queue is created. You can see the queue numbers in the \System32\msmq\storage\lqs<br />

directory.<br />

With FormatName:DIRECT=Protocol:MachineAddress\QueueName, you can specify the protocol<br />

that should be used to send the message. The HTTP protocol is supported since Message Queuing 3.0.<br />

FormatName:DIRECT=OS:MachineName\QueueName is another way to specify a queue using the<br />

format name. This way you don’t have to specify the protocol but still can use the machine name with<br />

the format name.<br />

sending a message<br />

You can use the Send method of the MessageQueue class to send a message to the queue. The object<br />

passed as an argument of the Send() method is serialized to the associated queue. The Send() method is<br />

overloaded so that a label <strong>and</strong> a MessageQueueTransaction object can be passed. Transactional behavior<br />

of Message Queuing is discussed later.<br />

The code example first checks if the queue exists. If it doesn’t exist, a queue is created. Then the queue is<br />

opened <strong>and</strong> the message Sample Message is sent to the queue using the Send() method.<br />

The pathname specifies a dot (just like a period) for the server name, which is the local system. Pathnames<br />

to private queues work only locally:<br />

using System;<br />

using System.Messaging;<br />

namespace Wrox.ProCSharp.Messaging<br />

{<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!