15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1364 ❘ ChaPTer 46 messAGe QueuinG<br />

MyNewPublicQueue is created on the local host. To create a private queue, the pathname must include<br />

Private$ ; for example, \Private$\MyNewPrivateQueue .<br />

After the Create() method is invoked, properties of the queue can be changed. For example, using the<br />

Label property, the label of the queue is set to Demo Queue . The sample program writes the path of<br />

the queue <strong>and</strong> the format name to the console. The format name is automatically created with a UUID that<br />

can be used to access the queue without the name of the server:<br />

using System;<br />

using System.Messaging;<br />

namespace Wrox.ProCSharp.Messaging<br />

{<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

using (var queue = MessageQueue.Create(@".\MyNewPublicQueue"))<br />

{<br />

queue.Label = "Demo Queue";<br />

Console.WriteLine("Queue created:");<br />

Console.WriteLine("Path: {0}", queue.Path);<br />

Console.WriteLine("FormatName: {0}", queue.FormatName);<br />

}<br />

}<br />

}<br />

}<br />

code snippet CreateMessageQueue/Program.cs<br />

Administrative privileges are required to create a queue. Usually, you cannot expect the<br />

user of your application to have administrative privileges. That’s why queues usually<br />

are created with installation programs. Later in this chapter, you see how message<br />

queues can be created with the MessageQueueInstaller class.<br />

finding a queue<br />

The pathname <strong>and</strong> the format name can be used to identify queues. To fi nd queues, you must differentiate<br />

between public <strong>and</strong> private queues. Public queues are published in the Active Directory. For these queues, it<br />

is not necessary to know the system where they are located. Private queues can be found only if the name of<br />

the system where the queue is located is known.<br />

You can fi nd public queues in the Active Directory domain by searching for the queue ’ s label, category,<br />

or format name. You can also get all queues on a machine. The class MessageQueue has static<br />

methods to search for queues: GetPublicQueuesByLabel() , GetPublicQueuesByCategory() , <strong>and</strong><br />

GetPublicQueuesByMachine() . The method GetPublicQueues() returns an array of all public queues in<br />

the domain:<br />

using System;<br />

using System.Messaging;<br />

namespace Wrox.ProCSharp.Messaging<br />

{<br />

class Program<br />

{<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!