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

RoutingService on Azure

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
18 Jul 2011CPOL19 min read 60.1K   429   20  
This article describes a design, implementation and usage of the WCF4 Routing Service hosted by Windows Azure.
//*****************************************************************************
//    Description.....Contract Model - LocalRepositoryConcole
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright © 2008 ATZ Consulting Inc. 
//                        
//    Date Created:    07/07/08
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    07/07/08    Roman Kiss     Initial Revision
//*****************************************************************************
//  
#region Namespaces
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Data;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Configuration;
using System.ServiceModel.Web;
using System.Web.Services;
using System.Windows.Forms;
using System.CodeDom;
using System.CodeDom.Compiler;
#endregion

namespace Simulator1
{


    public class WsdlHelper
    {
        #region GetWsdlMetadata
        public static WsdlImporter GetWsdlMetadata(XmlReader reader)
        {
            var serviceDescription = System.Web.Services.Description.ServiceDescription.Read(reader);
            MetadataSection metadataSection = new MetadataSection()
            {
                Dialect = MetadataSection.ServiceDescriptionDialect,
                Identifier = serviceDescription.TargetNamespace,
                Metadata = serviceDescription
            };
            var listOfSections = new List<MetadataSection>();
            listOfSections.Add(metadataSection);
            MetadataSet mds = new MetadataSet(listOfSections);

            if (mds == null)
                throw new Exception(string.Format("Cannot read metadaset"));

            return new WsdlImporter(mds);
        }
        public static WsdlImporter GetWsdlMetadata(string url)
        {
            MetadataSet mds = null;
            MetadataExchangeClientMode mode = MetadataExchangeClientMode.MetadataExchange;
            int maxReceivedMessageSize = 3000000;
            Uri address = new Uri(url);

            if (address.Scheme == "file")
            {
                if (File.Exists(url))
                {
                    // this is file for mex file
                    //using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(url)))
                    //using (XmlReader rd = XmlReader.Create(ms))
                    //{
                    //    mds = MetadataSet.ReadFrom(rd);
                    //}

                    // from wsdl file
                    var serviceDescription = System.Web.Services.Description.ServiceDescription.Read(url);
                    MetadataSection metadataSection = new MetadataSection()
                    {
                        Dialect = MetadataSection.ServiceDescriptionDialect,
                        Identifier = serviceDescription.TargetNamespace,
                        Metadata = serviceDescription
                    };
                    var listOfSections = new List<MetadataSection>();
                    listOfSections.Add(metadataSection);
                    mds = new MetadataSet(listOfSections);

                }
                else
                    throw new Exception(string.Format("File doesn't exist"));
            }
            else
            {
                System.ServiceModel.Channels.Binding mexBinding = null;
                if (string.Compare(address.Scheme, "http", StringComparison.OrdinalIgnoreCase) == 0)
                    mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();
                else if (string.Compare(address.Scheme, "https", StringComparison.OrdinalIgnoreCase) == 0)
                    mexBinding = MetadataExchangeBindings.CreateMexHttpsBinding();
                else if (string.Compare(address.Scheme, "net.tcp", StringComparison.OrdinalIgnoreCase) == 0)
                    mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
                else if (string.Compare(address.Scheme, "net.pipe", StringComparison.OrdinalIgnoreCase) == 0)
                    mexBinding = MetadataExchangeBindings.CreateMexNamedPipeBinding();
                else
                    throw new Exception(string.Format("Not supported schema '{0}' for metadata exchange", address.Scheme));

                if (mexBinding is WSHttpBinding)
                {
                    (mexBinding as WSHttpBinding).MaxReceivedMessageSize = maxReceivedMessageSize;
                    mode = MetadataExchangeClientMode.HttpGet;
                }
                else if (mexBinding is CustomBinding)
                    (mexBinding as CustomBinding).Elements.Find<TransportBindingElement>().MaxReceivedMessageSize = maxReceivedMessageSize;

                if (mexBinding != null)
                {
                    MetadataExchangeClient proxy = new MetadataExchangeClient(mexBinding);
                    proxy.ResolveMetadataReferences = true;
                    mds = proxy.GetMetadata(address, mode);
                }
                else
                    throw new Exception(string.Format("Not supported binding for metadata exchange"));
            }
            if (mds != null)
            {
                return new WsdlImporter(mds);
            }

            throw new Exception(string.Format("Cannot read metadaset"));
        }

        //public static WsdlImporter GetWsdlMetadata(string url)
        //{
        //    MetadataExchangeClientMode mode = MetadataExchangeClientMode.MetadataExchange;
        //    int maxReceivedMessageSize = 3000000;
        //    Uri address = new Uri(url);

        //    System.ServiceModel.Channels.Binding mexBinding = null;
        //    if (string.Compare(address.Scheme, "http", StringComparison.OrdinalIgnoreCase) == 0)
        //        mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();
        //    else if (string.Compare(address.Scheme, "https", StringComparison.OrdinalIgnoreCase) == 0)
        //        mexBinding = MetadataExchangeBindings.CreateMexHttpsBinding();
        //    else if (string.Compare(address.Scheme, "net.tcp", StringComparison.OrdinalIgnoreCase) == 0)
        //        mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
        //    else if (string.Compare(address.Scheme, "net.pipe", StringComparison.OrdinalIgnoreCase) == 0)
        //        mexBinding = MetadataExchangeBindings.CreateMexNamedPipeBinding();
        //    else
        //        throw new Exception(string.Format("Not supported schema '{0}' for metadata exchange"));

        //    if (mexBinding is WSHttpBinding)
        //    {
        //        (mexBinding as WSHttpBinding).MaxReceivedMessageSize = maxReceivedMessageSize;
        //        mode = MetadataExchangeClientMode.HttpGet;
        //    }
        //    else if (mexBinding is CustomBinding)
        //        (mexBinding as CustomBinding).Elements.Find<TransportBindingElement>().MaxReceivedMessageSize = maxReceivedMessageSize;
        //    else
        //        throw new Exception(string.Format("Not supported binding for metadata exchange"));

        //    MetadataExchangeClient proxy = new MetadataExchangeClient(mexBinding);
        //    proxy.ResolveMetadataReferences = true;
        //    MetadataSet mds = proxy.GetMetadata(address, mode);
        //    return new WsdlImporter(mds);
        //}
        #endregion
    }


    public class MessageHelper
    {

        public static string ShowMessage(System.ServiceModel.Channels.Message message)
        {
            string retVal = string.Empty;

            if (message != null)
            {
                using (MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue))
                using (StringWriter sw = new StringWriter())
                using (XmlTextWriter wr = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
                using (XmlDictionaryWriter dwr = XmlDictionaryWriter.CreateDictionaryWriter(wr))
                {
                    dwr.WriteStartDocument();
                    buffer.CreateMessage().WriteMessage(dwr);
                    dwr.WriteEndDocument();
                    dwr.Flush();

                    message = buffer.CreateMessage();
                    retVal = sw.ToString();
                }
            }

            return retVal;
        }
    }
}

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