Click here to Skip to main content
15,896,154 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.Diagnostics;
using System.Net.NetworkInformation;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;
using System.Linq;
using System.Configuration;

namespace RKiss.Tools.ServiceBusTester
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            ServiceHost host = null;

            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Form1 form = new Form1();

                try
                {
                    string uriAddressString = null;

                    string ports = ConfigurationManager.AppSettings["rangeOfPorts"];
                    int[] rangeOfPorts = string.IsNullOrEmpty(ports) ?
                        new int[] { 10100, 10101, 10102, 10103, 10104, 10105, 10106, 10107, 10108, 10109 } :
                        ports.Split(',').Select(n => Convert.ToInt32(n)).ToArray();

                    var usedPorts = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
                    for (int ii = 0; ii < rangeOfPorts.Length; ii++)
                    {
                        if (usedPorts.FirstOrDefault(p => p.Port == rangeOfPorts[ii]) == null)
                        {
                            uriAddressString = string.Format(@"http://localhost:{0}/sb", rangeOfPorts[ii]);
                            break;
                        }
                    };

                    if (string.IsNullOrEmpty(uriAddressString))
                        throw new Exception("Not available port in the range 10100-10109"); 

                    // interprocess communications
                    var endpointAddress = new EndpointAddress("net.pipe://localhost/ServiceBusTester_" + Process.GetCurrentProcess().Id);
                    var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
                    var se = new ServiceEndpoint(ContractDescription.GetContract(typeof(IGenericOneWayContract)), binding, endpointAddress);

                    // rest communication
                    var endpointAddress2 = new EndpointAddress(uriAddressString);
                    var binding2 = new WebHttpBinding();
                    var se2 = new ServiceEndpoint(ContractDescription.GetContract(typeof(IBrokerMessage)), binding2, endpointAddress2);
                    se2.Behaviors.Add(new WebHttpBehavior());

                    host = new ServiceHost(typeof(TesterService));
                    host.AddServiceEndpoint(se);
                    host.AddServiceEndpoint(se2);

                    host.Extensions.Add(form);
                    host.Open();

                    form.Tag = se2.Address.Uri;
                }
                catch (Exception ex)
                {
                    form.ShowText(ex.Message);
                }


                form.Text = string.Format("[{0}] Service Bus Tester", (form.Tag as Uri).Port);
                Application.Run(form);

            }
            finally
            {
                if (host != null && host.State == CommunicationState.Faulted)
                    host.Abort();
                else if (host != null && host.State == CommunicationState.Opened)
                    host.Close();
            }


            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
        }
    }
}

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