Skip Navigation LinksAzure Developers Guide > Azure Service Bus > Brokered Messaging > Managing Messaging Artifacts > Walkthrough: Creating a Simple Queue Management Tool

Training Courses

All course material is in English, and courses are delivered in English. Feel free to contact me for further information. cloudcasts.net@gmail.com

Walkthrough: Creating a Simple Queue Management Tool

When creating applications that use Service Bus messaging it makes sense to have a separate application or console to manage the messaging artifacts rather than creating them in the applications that send and receive messages.

This section will take a walk through the creation of a simple console application to manage queues in a Service Bus namespace. This application is a help when creating basic messaging applications but it does not provide support for setting properties on queues, or managing topics and subscriptions.

Creating the Solution

The solution is created as a C# console application named SimpleQueueManagementConsole. The target framework of the project is changed from .NET Framework 4 Client Profile to .NET Framework 4 and references to the following assemblies added.

·        Microsoft.ServiceBus

·        System.Configuration

·        System.Runtime.Serialization

Adding Configuration Settings

The management console must connect to a service bus namespace using a set of credentials. To prompting the user for credentials they will be placed in the app.config file.

<?xml version="1.0"?>

<configuration>

  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

  </startup>

 

  <appSettings>

    <add key="NameSpace" value="(enter value)"/>

    <add key="CredentialName" value="(enter value)"/>

    <add key="CredentialKey" value="(enter value)"/>

  </appSettings>

 

</configuration>

 

You will have to set the appropriate values for your Azure account. Be aware that storing these credentials in a configuration file is a potential security risk.

Adding a ManagementHelper Class

The next step is to add a helper class named ManagementHelper that will initialize a NamespaceManager member variable with the appropriate settings in the constructor.

 

namespace SimpleQueueManagementConsole

{

    class ManagementHelper

    {

        private NamespaceManager m_NamespaceManager;

 

        public ManagementHelper()

        {

            // Retrieve the account details from the configuration file.

            string nameSpace = ConfigurationManager.AppSettings["NameSpace"];

            string credentialName = ConfigurationManager.AppSettings["CredentialName"];

            string credentialKey = ConfigurationManager.AppSettings["CredentialKey"];

 

 

            // Create a token provider with the relevant credentials.

            TokenProvider credentials =

                TokenProvider.CreateSharedSecretTokenProvider

                (credentialName, credentialKey);

 

 

            // create a Uri for the service bus using the specified namespace

            Uri sbUri = ServiceBusEnvironment.CreateServiceUri

                ("sb", nameSpace, string.Empty);

 

            // Create a ServiceBusNamespaceClient for the specified namespace

            // using the specified credentials.

            m_NamespaceManager = new NamespaceManager (sbUri, credentials);

 

            // Set the timeout to 15 seconds.

            m_NamespaceManager.Settings.OperationTimeout = TimeSpan.FromSeconds(15);

 

            // Output the client address.

            Console.WriteLine("Service bus address {0}", m_NamespaceManager.Address);

        }

 

 

Once this is done methods can be added to create, delete, get and list queues.

 

public void CreateQueue(string queuePath)

{

    Console.Write("Creating queue {0}...", queuePath);

    m_NamespaceManager.CreateQueue(queuePath);           

    Console.WriteLine("Done!");

}

 

public void DeleteQueue(string queuePath)

{

    Console.Write("Deleting queue {0}...", queuePath);

    m_NamespaceManager.DeleteQueue(queuePath);

    Console.WriteLine("Done!");

}

 

public void GetQueue(string queuePath)

{

    QueueDescription queueDescription = m_NamespaceManager.GetQueue(queuePath);

           

    Console.WriteLine("Queue Path:                                   {0}",

        queueDescription.Path);

    Console.WriteLine("Queue MessageCount:                           {0}",

        queueDescription.MessageCount);

    Console.WriteLine("Queue SizeInBytes:                            {0}",

        queueDescription.SizeInBytes);

    Console.WriteLine("Queue RequiresSession:                        {0}",

        queueDescription.RequiresSession);

    Console.WriteLine("Queue RequiresDuplicateDetection:             {0}",

        queueDescription.RequiresDuplicateDetection);

    Console.WriteLine("Queue DuplicateDetectionHistoryTimeWindow:    {0}",

        queueDescription.DuplicateDetectionHistoryTimeWindow);

    Console.WriteLine("Queue LockDuration:                           {0}",

        queueDescription.LockDuration);

    Console.WriteLine("Queue DefaultMessageTimeToLive:               {0}",

        queueDescription.DefaultMessageTimeToLive);

    Console.WriteLine("Queue EnableDeadLetteringOnMessageExpiration: {0}",

        queueDescription.EnableDeadLetteringOnMessageExpiration);

    Console.WriteLine("Queue EnableBatchedOperations:                {0}",

        queueDescription.EnableBatchedOperations);

    Console.WriteLine("Queue MaxSizeInMegabytes:                     {0}",

        queueDescription.MaxSizeInMegabytes);

    Console.WriteLine("Queue MaxDeliveryCount:                       {0}",

        queueDescription.MaxDeliveryCount);

    Console.WriteLine("Queue IsReadOnly:                             {0}",

        queueDescription.IsReadOnly);

}

 

public void ListQueues()

{

    IEnumerable<QueueDescription> queueDescriptions = m_NamespaceManager.GetQueues();

    Console.WriteLine("Lisitng queues...");

    foreach (QueueDescription queueDescription in queueDescriptions)

    {

        Console.WriteLine("\t{0}", queueDescription.Path);

    }

    Console.WriteLine("Done!");

}

 

 

Processing the Commands

The console application will process the commands that the user enters and call the appropriate methods on the helper class. The following code in the Main method will implement this.

 

static void Main(string[] args)

{

    ManagementHelper helper = new ManagementHelper();

 

    bool done = false;

    do

    {

        Console.ForegroundColor = ConsoleColor.Cyan;

        Console.Write(">");

        string commandLine = Console.ReadLine();

        Console.ForegroundColor = ConsoleColor.Magenta;

        string[] commands = commandLine.Split(' ');

 

        try

        {

            if (commands.Length > 0)

            {

                switch (commands[0])

                {

                    case "createqueue":

                    case "cq":

                        if (commands.Length > 1)

                        {

                            helper.CreateQueue(commands[1]);

                        }

                        else

                        {

                            Console.ForegroundColor = ConsoleColor.Yellow;

                            Console.WriteLine("Queue path not specified.");

                        }

                        break;

                    case "listqueues":

                    case "lq":

                        helper.ListQueues();

                        break;

                    case "getqueue":

                    case "gq":

                        if (commands.Length > 1)

                        {

                            helper.GetQueue(commands[1]);

                        }

                        else

                        {

                            Console.ForegroundColor = ConsoleColor.Yellow;

                            Console.WriteLine("Queue path not specified.");

                        }

                        break;

                    case "deletequeue":

                    case "dq":

                        if (commands.Length > 1)

                        {

                            helper.DeleteQueue(commands[1]);

                        }

                        else

                        {

                            Console.ForegroundColor = ConsoleColor.Yellow;

                            Console.WriteLine("Queue path not specified.");

                        }

                        break;

                    case "exit":

                        done = true;

                        break;

                    default:

                        break;

                }

            }

        }

        catch (Exception ex)

        {

            Console.WriteLine();

            Console.ForegroundColor = ConsoleColor.Red;

            Console.WriteLine(ex.Message);

        }

 

    } while (!done);

 

}

 

 

Testing the Console

The queue management console can now be tested.

Speaking Engagements