Click here to Skip to main content
15,886,110 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 89.9K   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.ComponentModel;
using System.Threading;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.ServiceBus;
using System.Configuration;
using Microsoft.ServiceBus.Messaging;

namespace RKiss.Tools.ServiceBusTester.Dialogs
{
    public partial class NamespaceDialog : Form
    {
        string servicebus = "servicebus.windows.net";

        public NamespaceDialog(List<ServiceBusConnection> sbNamespaces, string restEndpoint)
        {
            InitializeComponent();

            this.linkLabelREST.Text = restEndpoint;

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

            if (sbNamespaces != null)
            {
                foreach (var item in sbNamespaces)
                {
                    this.dataGridViewNamespaces.Rows.Add(this.imageListDialog.Images[0], item.Namespace, item.IssuerName, item.IssuerSecret);
                }
                this.dataGridViewNamespaces.Rows[sbNamespaces.Count].Cells[this.ColumnStatus.Name].Value = this.imageListDialog.Images[0];
                this.RefreshWorker_Namespace();
            }
            else
            {
                this.dataGridViewNamespaces.Rows[0].Cells[this.ColumnStatus.Name].Value = this.imageListDialog.Images[0];
            }
        }

        public List<ServiceBusConnection> ServiceBusNamespaces
        {
            get
            {
                List<ServiceBusConnection> sbNamespaces = new List<ServiceBusConnection>();
                foreach (DataGridViewRow row in this.dataGridViewNamespaces.Rows)
                {
                    if (row.IsNewRow == false)
                    {
                        sbNamespaces.Add(new ServiceBusConnection
                        {
                            Namespace = row.Cells[this.ColumnNamespace.Name].Value as string,
                            IssuerName = row.Cells[this.ColumnIssuerName.Name].Value as string,
                            IssuerSecret = row.Cells[this.ColumnIssuerSecret.Name].Value as string,
                        });
                        row.Cells[this.ColumnStatus.Name].ErrorText = string.Empty;
                    }
                }
                return sbNamespaces;
            }
        }

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

        private void RefreshWorker_Namespace()
        {
            ThreadPool.QueueUserWorkItem(delegate(object state)
            {
                List<ServiceBusConnection> sbNamespaces = null;
                this.InvokeEx(delegate()
                {                    
                    sbNamespaces = this.ServiceBusNamespaces;
                    this.dataGridViewNamespaces.Enabled = false;
                    this.buttonOK.Enabled = false;
                    this.buttonRefresh.Enabled = false;
                    this.buttonRefresh.Text = "Loading";
                });

                bool bError = false;
                using (var progress = new ProgressButton(this, this.buttonRefresh, 1, 0))
                {

                    int rowIndex = 0;
                    foreach (ServiceBusConnection item in sbNamespaces)
                    {
                        string errorText = this.GetNamespaceClient(item);

                        this.InvokeEx(delegate()
                        {
                           bError = errorText == null ? bError : true;
                           this.dataGridViewNamespaces.Rows[rowIndex].Cells[this.ColumnStatus.Name].ErrorText = errorText == null ? string.Empty : errorText;
                        });

                        rowIndex++;
                    }
                }

                this.InvokeEx(delegate()
                {
                    this.buttonOK.Enabled = !bError;
                    this.dataGridViewNamespaces.Enabled = true;
                    this.buttonRefresh.Text = "Refresh";
                    this.buttonRefresh.Enabled = true;
                });
            });
        } 
 
        private string GetNamespaceClient(ServiceBusConnection ns)
        {
            try
            {
                var token = ServiceBusHelper.GetTokenProvider(ns.IssuerName, ns.IssuerSecret);
                string namespaceaddress = string.Format("sb://{0}.{1}", ns.Namespace, servicebus);

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

                // check for dummy queue for this specific namespace
                namespaceClient.QueueExists("abcd");

                return null;
            }
            catch (Microsoft.ServiceBus.AuthorizationFailedException ex)
            {
                return ex.Message;
            }
            catch (Microsoft.ServiceBus.InvalidRequestException ex)
            {
                return ex.Message;
            }
            catch (Exception ex)
            {
                return ex.InnerException == null ? ex.Message : ex.InnerException.Message;
            }
        }

        private void dataGridViewNamespaces_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            this.buttonOK.Enabled = false;
            this.dataGridViewNamespaces.Rows[e.RowIndex].Cells[this.ColumnStatus.Name].Value = this.imageListDialog.Images[0];
        }

        private void dataGridViewNamespaces_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            this.buttonOK.Enabled = false;
        }

    
 
    }
    
}

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