Click here to Skip to main content
15,896,606 members
Articles / Hosted Services / Azure

Azure Service Bus Tester

Rate me:
Please Sign up or sign in to vote.
4.96/5 (16 votes)
27 Dec 2014CPOL28 min read 90.1K   2.9K   24  
This article describes the design and implementation of the small tool, tester for Windows Azure Service Bus Messaging.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Microsoft.ServiceBus;

namespace RKiss.Tools.ServiceBusTester.Dialogs
{
    public partial class PublisherDialog : Form
    {
        List<ServiceBusConnection> namespaces = null;
        string defaultTopic = null;
        string servicebus = "servicebus.windows.net";

        public PublisherDialog(List<ServiceBusConnection> listOfConnections, ServiceBusDefaults defaults)
        {
            InitializeComponent();

            if (ConfigurationManager.AppSettings.AllKeys.Contains("servicebus"))
            {
                servicebus = ConfigurationManager.AppSettings["servicebus"];
            }

            this.textBoxPublisherId.Text = Guid.NewGuid().ToString();
            this.namespaces = listOfConnections;
            this.defaultTopic = defaults.Topic;
         
            string[] ns = (from n in namespaces select n.Namespace).ToArray<string>();

            if (ns != null && ns.Count() > 0)
            {
                this.comboBoxNamespace.Items.AddRange(ns);
                this.comboBoxNamespace.SelectedItem = ns.Contains(defaults.Namespace) ? defaults.Namespace : ns[0];
                this.comboBoxNamespace.SelectedIndexChanged += new EventHandler(comboBoxNamespace_SelectedIndexChanged);
              
                this.comboBoxTopic.Items.Add(this.defaultTopic);
                this.comboBoxTopic.SelectedIndex = 0;

                this.buttonOK.Enabled = false;
                this.RefreshWorker_Namespace();
            }
            else
            {
                MessageBox.Show("No Connection to the Service Bus", "Add Publisher", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = System.Windows.Forms.DialogResult.Abort;
                this.Close();
            }           
        }

        void comboBoxNamespace_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.RefreshWorker_Namespace();
        }

        #region Public
        public string NamespaceAddress
        {
            get { return string.Format("sb://{0}.{1}", this.comboBoxNamespace.Text, servicebus); }
        }

        public string TopicAddress
        {
            get { return string.Format("sb://{0}.{1}/{2}", this.comboBoxNamespace.Text, this.servicebus, this.comboBoxTopic.Text); }
        }

        public string PublisherName
        {
            get { return string.Format("{0}.{1}.{2}", this.comboBoxNamespace.Text, this.comboBoxTopic.Text, this.textBoxPublisherId.Text); }
        }

        public ServiceBusConnection ServiceBusConnection
        {
            get { return this.namespaces.Where(c => c.Namespace == this.comboBoxNamespace.Text).FirstOrDefault(); }
        }
        #endregion

        private string[] GetTopics(string sbNamespace)
        {
            try
            {
                var ns = namespaces.FirstOrDefault(n => n.Namespace == sbNamespace);
                if (ns == null)
                    throw new Exception(string.Format("Please, make a connection to the '{0}' namespace", sbNamespace));

                var token = TokenProvider.CreateSharedSecretTokenProvider(ns.IssuerName, ns.IssuerSecret);
                string namespaceaddress = string.Format("sb://{0}.{1}", sbNamespace, this.servicebus);

                var namespaceClient = new Microsoft.ServiceBus.NamespaceManager(namespaceaddress, token);
                var topics = namespaceClient.GetTopics();

                return (from t in topics select t.Path).ToArray();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Get Topics", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            this.RefreshWorker_Namespace();
        }

        private void RefreshWorker_Namespace()
        {
            ThreadPool.QueueUserWorkItem(delegate(object state)
            {
                string selectedNamespace = null;
                string selectedTopic = null;

                this.InvokeEx(delegate()
                {
                    selectedNamespace = this.comboBoxNamespace.Text;
                    selectedTopic = this.comboBoxTopic.Text;

                    this.groupBoxSB.Enabled = false;
                    this.buttonOK.Enabled = false;
                    this.buttonRefresh.Enabled = false;
                    this.buttonRefresh.Text = "Loading";

                    this.comboBoxTopic.Items.Clear();
                });

                using (var progress = new ProgressButton(this, this.buttonRefresh, 1, 0))
                {
                    var topics = this.GetTopics(selectedNamespace);
                    if (topics != null && topics.Count() > 0)
                    {
                        this.InvokeEx(delegate()
                        {
                            this.comboBoxTopic.Items.AddRange(topics);
                            this.comboBoxTopic.SelectedItem = this.comboBoxTopic.Items.Contains(selectedTopic) ? selectedTopic : this.comboBoxTopic.Items[0];
                            selectedTopic = this.comboBoxTopic.Text;

                            this.buttonOK.Enabled = this.comboBoxTopic.SelectedItem != null;
                        });
                    }

                    this.InvokeEx(delegate()
                    {
                        this.groupBoxSB.Enabled = true;
                        this.buttonRefresh.Text = "Refresh";
                        this.buttonRefresh.Enabled = true;
                    });
                }
            });
        }     
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions