Click here to Skip to main content
15,884,176 members
Articles / DevOps / Load Testing

VirtualService for ESB

Rate me:
Please Sign up or sign in to vote.
4.90/5 (32 votes)
19 Feb 2008CPOL37 min read 96.6K   1.2K   98  
This article describes the design, implementation, and usage of VirtualService for the Enterprise Service Bus, using the Microsoft .NET FX 3.5 technology.
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
using System.Xml;
using System.IO;
//
using RKiss.ESB;
using RKiss.WSTransfer;
using RKiss.WSTransferActivity;

namespace WorkflowLibrary1
{
	public partial class Workflow6 : SequentialWorkflowActivity
	{
        public System.ServiceModel.Channels.Message _message = default(System.ServiceModel.Channels.Message);
        public System.ServiceModel.Channels.Message _returnValue = default(System.ServiceModel.Channels.Message);

        private void InMessage(object sender, EventArgs e)
        {
            //this.Dispatcher.ChannelToken.EndpointName = "dada2";
            //this.Dispatcher.ServiceOperationInfo.ContractType = Type.GetType("Contract.ITest");

            Console.WriteLine("\n[{0}] **** ProcessMessage - mediation IN ****", this.WorkflowInstanceId);
            Console.WriteLine("\n" + _message);

            // xslt mediation
            if (OperationContext.Current != null)
            {
                ServiceConfigDataService service = OperationContext.Current.Host.Extensions.Find<ServiceConfigDataService>();
                string xslt = service.ServiceConfigData.Xslt;
                MediatorElementCollection mediators = ServiceMediatorsSection.GetInputMediators(xslt, OperationContext.Current.IncomingMessageHeaders.Action);
                if (mediators != null)
                {
                    for (int ii = 0; ii < mediators.Count; ii++)
                    {
                        Console.WriteLine("\n input mediator '{0}': xpath={1}, xslt={2}", mediators[ii].Name, mediators[ii].Xpath, mediators[ii].Xslt);
                    }
                }
            }            
        }

        private void BeforeDispatcher(object sender, SendActivityEventArgs e)
        {
            Console.WriteLine("\n BeforeDispatcher '{0}'", (sender as Activity).Name);
            //e.SendActivity.ChannelToken.EndpointName = "aaaaaa";
            //e.SendActivity.CustomAddress = "aaaaa";
        }
        
        private void OutMessage(object sender, EventArgs e)
        {
            // post-processing
            List<AddressHeader> list = new List<AddressHeader>();
            list.Add(AddressHeader.CreateAddressHeader(ResourceDescriptor.DataContractName, ResourceDescriptor.DataContractNamespace, new ResourceDescriptor()));
            AddressHeaderCollection ahc = new AddressHeaderCollection(new AddressHeaderCollection(list));
            Uri uriTo = OperationContext.Current.IncomingMessageHeaders.To;
            EndpointAddress resourceEndpointAddress = new EndpointAddress(uriTo, (EndpointIdentity)null, ahc);

            // create stream
            MemoryStream ms = new MemoryStream();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            XmlWriter xw = XmlWriter.Create(ms, settings);
            resourceEndpointAddress.WriteTo(AddressingVersion.WSAddressing10, xw, WSTransfer.ElementNames.ResourceCreated, WSTransfer.NamespaceUri);
            xw.Flush();
            ms.Position = 0;

            // create message body from stream
            XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
            _returnValue = Message.CreateMessage(MessageVersion.Default, WSTransfer.CreateResponseAction, xdr);

            MessageHeader hdr = MessageHeader.CreateHeader(WSResourceTransfer.ElementNames.ResourceTransfer, WSResourceTransfer.NamespaceUri, null);
            _returnValue.Headers.Add(hdr);
        }

        private void UnTypedMessage(object sender, EventArgs e)
        {
            Console.WriteLine("\nAction = {0}\n", _message.Headers.Action);
        }

        private void OutMessage2(object sender, EventArgs e)
        {

            Console.WriteLine("\n **** ProcessMessage - mediation OUT ****");

            // xslt mediation
            if (OperationContext.Current != null)
            {
                ServiceConfigDataService service = OperationContext.Current.Host.Extensions.Find<ServiceConfigDataService>();
                string xslt = service.ServiceConfigData.Xslt;
                MediatorElementCollection mediators = ServiceMediatorsSection.GetOutputMediators(xslt, OperationContext.Current.IncomingMessageHeaders.Action);
                if (mediators != null)
                {
                    for (int ii = 0; ii < mediators.Count; ii++)
                    {
                        Console.WriteLine("\n output mediator '{0}': xpath={1}, xslt={2}", mediators[ii].Name, mediators[ii].Xpath, mediators[ii].Xslt);
                    }
                }
            }

            // create untyped message
            MemoryStream ms = new MemoryStream();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            XmlWriter xw = XmlWriter.Create(ms, settings);

            xw.WriteStartElement("Ping3Response", "http://tempuri.org/");
            xw.WriteElementString("Ping3Result", "111111111111111111111111111111");
            xw.WriteEndElement();
            xw.Flush();
            ms.Position = 0;

            XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
            _returnValue = Message.CreateMessage(MessageVersion.Default, @"http://tempuri.org/ITest/Ping3Response", xdr);
        }

        private void MexMessage(object sender, EventArgs e)
        {           
            Console.WriteLine(" +++ Generating mex document +++");
        }

        private void Execute_Mediator(object sender, EventArgs e)
        {
            // create untyped message
            MemoryStream ms = new MemoryStream();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            XmlWriter xw = XmlWriter.Create(ms, settings);

            xw.WriteStartElement("Ping3Response", "http://tempuri.org/");
            xw.WriteElementString("Ping3Result", "Hello World");
            xw.WriteEndElement();
            xw.Flush();
            ms.Position = 0;

            XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
            _returnValue = Message.CreateMessage(MessageVersion.Default, @"http://tempuri.org/ITest3/Ping3Response", xdr);
            Console.WriteLine(_returnValue);          
        }      
	}
}

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