Click here to Skip to main content
15,888,527 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.
//*****************************************************************************
//    Description.....Service Bus Tester
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright © 2011 ATZ Consulting Inc. (see included license.rtf file)         
//                        
//    Date Created:    11/11/11
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    11/11/11   Roman Kiss     Initial Revision
//
//*****************************************************************************
//
#region Namespaces
using System;
using System.Diagnostics;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using Microsoft.ServiceBus.Messaging;
#endregion

namespace RKiss.Tools.ServiceBusTester
{
    [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
    public class VirtualService : IGenericOneWayContract
    {       
        public void ProcessMessage(System.ServiceModel.Channels.Message msg)
        {
            Trace.WriteLine("VirtualService: *** Message has been received ***");
            Trace.WriteLine(msg.ToString());

            ChannelFactory<IGenericOneWayContract> factory = null;

            //var m = (OperationContext.Current.IncomingMessageProperties[BrokeredMessageProperty.Name] as BrokeredMessageProperty).Message;
            //var rc = m.GetType().GetProperty("ReceiveContext", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(m, null);
            //var mr = rc.GetType().GetProperty("MessageReceiver").GetValue(rc, null) as MessageReceiver;
            //ReceiveMode mode = mr.Mode;
            //if (mode == ReceiveMode.PeekLock)
            //{
            //    //m.Abandon();
            //    m.Complete();
            //}

            try
            {
                var config = OperationContext.Current.Host.Extensions.Find<ConsumerConfigData>();
                if (config == null)
                    throw new Exception("Fatal error: Missing ServiceConfigData extension object");

                var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
                var se = new ServiceEndpoint(ContractDescription.GetContract(typeof(IGenericOneWayContract)), binding, new EndpointAddress(config.TesterAddress));
                
                factory = new ChannelFactory<IGenericOneWayContract>(se);              
                var channel = factory.CreateChannel();

                using (var scope = new OperationContextScope((IContextChannel)channel))
                {
                    if (msg.Version == MessageVersion.None)
                    {
                        var message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "*", msg.GetReaderAtBodyContents().ReadSubtree());

                        message.Headers.Add(MessageHeader.CreateHeader(BrokeredMessageProperty.Name, BrokeredMessagePropertyExtension.XName.NamespaceName, (msg.Properties[BrokeredMessageProperty.Name] as BrokeredMessageProperty).CopyTo()));
                        message.Headers.Add(MessageHeader.CreateHeader(ConsumerConfigData.XName.LocalName, ConsumerConfigData.XName.NamespaceName, config));                     
                        channel.ProcessMessage(message);
                    }
                    else
                    {
                        if (msg.Properties.ContainsKey(BrokeredMessageProperty.Name))
                        {
                            OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(BrokeredMessageProperty.Name, BrokeredMessagePropertyExtension.XName.NamespaceName, (msg.Properties[BrokeredMessageProperty.Name] as BrokeredMessageProperty).CopyTo()));
                        }
                        OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(ConsumerConfigData.XName.LocalName, ConsumerConfigData.XName.NamespaceName, config));
                        channel.ProcessMessage(msg);
                    }
                }
                factory.Close();
                
                Trace.WriteLine("VirtualService: --- Message has been sent to tester ---");

            }
            catch (CommunicationException ex)
            {
                if (factory != null)
                {
                    if (factory.State == CommunicationState.Faulted)
                        factory.Abort();
                    else if (factory.State != CommunicationState.Closed)
                        factory.Close();
                    factory = null;
                }
                Trace.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                if (factory != null)
                {
                    if (factory.State == CommunicationState.Faulted)
                        factory.Abort();
                    else if (factory.State != CommunicationState.Closed)
                        factory.Close();
                    factory = null;
                }
                Trace.WriteLine(ex.Message);
            }

        }
    }
}

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