Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Dynamic Binding Using the Factory Pattern

, 1 Nov 2009 CPOL
Using the Factory design pattern to hide dynamic binding and use configuration strings to determine which classes should be instantiated.
/*************************************************************************
 *
 * Copyright 2009 Richard (Rick) Marvin Wycoff, All Rights Reserved
 * DO NOT ALTER OR REMOVE THIS COPYRIGHT NOTICE.
 *
 * NOTICE: You are permitted to use, modify, and distribute this file in 
 * accordance with the terms of the license agreement accompanying it.  
 * Any existing copyright or authorship information in any given source
 * file must remain intact.
 *
 **************************************************************************/

using System;
using System.ServiceModel;


namespace Wycoff.Activation.WcfHttp
{
    /// <summary>
    /// Creates a remote object using WCF over HTTP
    /// </summary>
    /// <typeparam name="T">
    /// The type of the channel object.
    /// </typeparam>
    public class WcfHttpActivator<T> : Locator.ILocatorActivator
    {
        #region ILocatorActivator Members

        /// <summary>
        /// Creates a proxy to the remote object.
        /// </summary>
        /// <param name="type">Note Used</param>
        /// <param name="uri">
        /// Uri string in the format:  
        /// ActivatorMapKey://host[:port]/RemoteTheService<br />
        /// [ ] - means optional
        /// <remarks>
        /// Since this is a generic class, a different instance of
        /// this class will have to be provided to the Locator for each
        /// type of remote object.  The ActivatorMapKey should represent
        /// the User Defined string identifier for a type so the Locator
        /// can find the correct activator.  The ActivatorMapKey will then
        /// be replace with http to make the appropriate WCF connection.
        /// </remarks>
        /// </param>
        /// <returns>
        /// Proxy to the remote object.
        /// </returns>
        public object Activate(Type type, Uri uri)
        {
            string correctedUri = null;
            if (uri.IsDefaultPort)
            {
                correctedUri = string.Format("http://{0}{1}",
                 uri.Host, uri.PathAndQuery);
            }
            else
            {
                correctedUri = string.Format("http://{0}:{1}{2}",
                 uri.Host, uri.Port, uri.PathAndQuery);
            }

            BasicHttpBinding binding = new BasicHttpBinding();
            EndpointAddress address = new EndpointAddress(correctedUri);
            object proxy = ChannelFactory<T>.CreateChannel(binding, address);

            return proxy;
        }

        #endregion
    }
}

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)

Share

About the Author

Richard (Rick) Wycoff
Software Developer (Senior)
United States United States
Rick's software passion is developing "invisible” software components, the kind that no one ever thinks about because they just do what they are supposed to do. He believes in standards, design patterns, and designing components for extensibility and reuse.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150302.1 | Last Updated 1 Nov 2009
Article Copyright 2009 by Richard (Rick) Wycoff
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid