Click here to Skip to main content
15,886,723 members
Articles / Programming Languages / C#

Dynamic Binding Using the Factory Pattern

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
1 Nov 2009CPOL11 min read 25.9K   180   16  
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;
using System.ServiceModel.Description;

using Wycoff.Shared;

namespace WcfHttpHost
{
    /// <summary>
    /// ITheService implementation that will be served by this process.
    /// </summary>
    public class WcfRemoteService : ITheService
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public WcfRemoteService() { }

        #region ITheClass Members

        /// <summary>
        /// ITheService implementation method.
        /// </summary>
        /// <param name="name">
        /// Name of person to which the salutation should be addressed
        /// </param>
        /// <returns>
        /// Personalized salutation
        /// </returns>
        public string Hello(string name)
        {
            return string.Format("Saying hello to {0} from the WCF server", name);
        }

        #endregion
    }

    /// <summary>
    /// Main class for the Server Activated Object host application
    /// </summary>
    class Program
    {
        /// <summary>
        /// Static main for the SOA host application
        /// </summary>
        /// <param name="args">
        /// Not used
        /// </param>
        static void Main(string[] args)
        {
            // We did not separate contract from implementation.
            // Therefore service and contract are the same in this example.
            Type serviceType = typeof(WcfRemoteService);
            
            ServiceHost host = new ServiceHost(serviceType, 
                new Uri[] { new Uri("http://localhost:8080/") } );
            
            // Add behavior for our MEX endpoint
            ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
            behavior.HttpGetEnabled = true;
            host.Description.Behaviors.Add(behavior);
            
            // Create basicHttpBinding endpoint at http://localhost:8080/RemoteTheService/
            host.AddServiceEndpoint(typeof(ITheService), new BasicHttpBinding(), "RemoteTheService");
            // Add MEX endpoint at http://localhost:8080/MEX/
            host.AddServiceEndpoint(typeof(IMetadataExchange), 
                new BasicHttpBinding(), "MEX");
            
            // open the host
            host.Open();

            // Keep running until we are done
            Console.WriteLine("WCF Service is ready, press any key to terminate.");
            Console.ReadKey();

            // close the host.
            host.Close();
        }
    }
}

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
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.

Comments and Discussions