Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / Win32

Routing Manager for WCF4

Rate me:
Please Sign up or sign in to vote.
5.00/5 (38 votes)
29 Apr 2010CPOL18 min read 108.3K   2.5K   92  
This article describes a design, implementation and usage of the Custom Routing Manager for managing messages via Routing Service built-in .Net 4 Technology.
//*****************************************************************************
//    Description.....Routing Manager
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright © 2005 ATZ Consulting Inc.     
//                        
//    Date Created:    03/03/10
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    03/03/10    Roman Kiss     Initial Revision
//
//*****************************************************************************
//  
#region Namespaces
using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Description;
#endregion

namespace LocalRepository
{
    [ServiceContract(Namespace = "urn:rkiss/2010/09/ms/core/routing", SessionMode = SessionMode.Allowed)]
    public interface IRoutingManager
    {
        [OperationContract(IsOneWay = true)]
        void Refresh(string machineName, string applicationName);

        [OperationContract]
        void Set(string machineName, string applicationName, RoutingMetadata metadata);

        [OperationContract]
        void Reset(string machineName, string applicationName);

        [OperationContract]
        string GetStatus(string machineName, string applicationName);
    }



    public class Proxy
    {
        public static void Refresh(string address, string machineName, string applicationName)
        {
            ChannelFactory<IRoutingManager> factory = null;
            try
            {
                ServiceEndpoint se = new ServiceEndpoint(ContractDescription.GetContract(typeof(IRoutingManager)), new BasicHttpBinding(BasicHttpSecurityMode.None), new EndpointAddress(address));
                factory = new ChannelFactory<IRoutingManager>(se);

                IRoutingManager channel = factory.CreateChannel();
                channel.Refresh(machineName, applicationName);
                ((IClientChannel)channel).Close();
                factory.Close();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("[{0}] Refresh failed to delivery, error = {1}", applicationName, ex.Message));

                if (factory != null)
                {
                    if (factory.State == CommunicationState.Faulted)
                        factory.Abort();
                    else
                        factory.Close();
                    factory = null;
                }
                throw ex;
            }
   
        }

        public static void Set(string address, string machineName, string applicationName, RoutingMetadata metadata)
        {
            ChannelFactory<IRoutingManager> factory = null;
            try
            {
                ServiceEndpoint se = new ServiceEndpoint(ContractDescription.GetContract(typeof(IRoutingManager)), new BasicHttpBinding(BasicHttpSecurityMode.None), new EndpointAddress(address));
                factory = new ChannelFactory<IRoutingManager>(se);

                IRoutingManager channel = factory.CreateChannel();
                channel.Set(machineName, applicationName, metadata);
                ((IClientChannel)channel).Close();
                factory.Close();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("[{0}] Set failed to delivery, error = {1}", applicationName, ex.Message));

                if (factory != null)
                {
                    if (factory.State == CommunicationState.Faulted)
                        factory.Abort();
                    else
                        factory.Close();
                    factory = null;
                }
                throw ex;
            }
        }

        public static string GetStatus(string address, string machineName, string applicationName)
        {
            ChannelFactory<IRoutingManager> factory = null;
            try
            {
                ServiceEndpoint se = new ServiceEndpoint(ContractDescription.GetContract(typeof(IRoutingManager)), new BasicHttpBinding(BasicHttpSecurityMode.None), new EndpointAddress(address));
                factory = new ChannelFactory<IRoutingManager>(se);

                IRoutingManager channel = factory.CreateChannel();
                string retVal = channel.GetStatus(machineName, applicationName);
                ((IClientChannel)channel).Close();
                factory.Close();
                return retVal;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("[{0}] GetStatus failed to delivery, error = {1}", applicationName, ex.Message));

                if (factory != null)
                {
                    if (factory.State == CommunicationState.Faulted)
                        factory.Abort();
                    else
                        factory.Close();
                    factory = null;
                }
                throw ex;
            }
        }
    }  
}

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