Click here to Skip to main content
15,886,835 members
Articles / Desktop Programming / WPF

Extending WCF - Part II

Rate me:
Please Sign up or sign in to vote.
4.96/5 (39 votes)
21 Jan 2010CPOL4 min read 114.3K   2.5K   71  
An article on using compression/decompression of WCF data
/**
 * File name: ExtendedServiceHost.cs 
 * Author: Mosfiqur.Rahman
 * Date: 11/3/2009 12:47:44 PM format: MM/dd/yyyy
 * 
 * 
 * Modification history:
 * Name				Date					Desc
 * 
 *  
 * Version: 1.0
 * */

#region Using Directives

using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Description;

#endregion

namespace WcfExtensions
{
    /// <summary>
    /// Summary of this class.
    /// </summary>
    public class ExtendedServiceHost:ServiceHost
    {
        #region Member Variables

        #endregion

        #region Constructors

        public ExtendedServiceHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        public ExtendedServiceHost(Type serviceType)
            : base(serviceType)
        {
        }

        #endregion

        #region Properties

        #endregion

        #region Methods

        //Overriding ApplyConfiguration() allows us to 
        //alter the ServiceDescription prior to opening
        //the service host. 
        protected override void ApplyConfiguration()
        {
            try
            {
                //First, we call base.ApplyConfiguration()
                //to read any configuration that was provided for
                //the service we're hosting. After this call,
                //this.ServiceDescription describes the service
                //as it was configured.
                base.ApplyConfiguration();

                //Now that we've populated the ServiceDescription, we can reach into it
                //and do interesting things (in this case, we'll add an instance of
                //ServiceMetadataBehavior if it's not already there.
                var mexBehavior = Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (mexBehavior == null)
                {
                    mexBehavior = new ServiceMetadataBehavior();
                    Description.Behaviors.Add(mexBehavior);
                }
                else
                {
                    //Metadata behavior has already been configured, 
                    //so we don't have any work to do.
                    return;
                }

                //Add a metadata endpoint at each base address
                //using the "/mex" addressing convention
                foreach (var baseAddress in BaseAddresses)
                {
                    if (baseAddress.Scheme == Uri.UriSchemeHttp)
                    {
                        mexBehavior.HttpGetEnabled = true;
                        AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                           MetadataExchangeBindings.CreateMexHttpBinding(),
                                           "mex");
                    }
                    else if (baseAddress.Scheme == Uri.UriSchemeHttps)
                    {
                        mexBehavior.HttpsGetEnabled = true;
                        AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                           MetadataExchangeBindings.CreateMexHttpsBinding(),
                                           "mex");
                    }
                    else if (baseAddress.Scheme == Uri.UriSchemeNetPipe)
                    {
                        AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                           MetadataExchangeBindings.CreateMexNamedPipeBinding(),
                                           "mex");
                    }
                    else if (baseAddress.Scheme == Uri.UriSchemeNetTcp)
                    {
                        AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                           MetadataExchangeBindings.CreateMexTcpBinding(),
                                           "mex");
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.Write(ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

        #endregion

    }
}

// end of namespace

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
Web Developer KAZ Software Limited
Bangladesh Bangladesh

Comments and Discussions