Sending .NET configuration section over the wire using WCF

7 בספטמבר 2009


Have you ever needed to implement configuration sections for the application configuration file? Well probably you have.

Let's say you want to read the configuration section from your *.config file and send it thru WCF service. The default behavior when working with configuration object is use the System.Configuration namespace that doesn't support WCF.

So the question is what can we do?

A nice solution is to use the IConfigurationSectionHandler interface and mark the section implementation as a DataContract.

It will be something like:


    public class MyConfigurationSectionHandler : IConfigurationSectionHandler


        #region IConfigurationSectionHandler Members


        public object Create(object parent, object configContext, System.Xml.XmlNode section)


            DataContractSerializer xs = new DataContractSerializer(typeof(MyConfigurationSectionHandler));


            using (XmlNodeReader xnr = new XmlNodeReader(section))




                    var configSection = xs.ReadObject(xnr, false) as MyConfigurationSectionHandler;

                    if (configSection != null)


                        Applications = configSection.Applications;


                    return this;


                catch (Exception ex)


                    string s = ex.Message;

                    Exception iex = ex.InnerException;

                    while (iex != null)


                        s += "; " + iex.Message;

                        iex = iex.InnerException;


                    throw new ConfigurationErrorsException(

                    "Unable to deserialize an object of type \'" + GetType().FullName +

                    "\' from  the < " + section.Name + "> configuration section: " +










        [DataMember(Name = "applications")]

        public Application[] Applications { get;set; }



    [DataContract(Name = "application")]

    public class Application


        [DataMember(Name = "name")]

        public string Name { get; set; }


An example for an appropriate configuration section can be something like:

<?xml version="1.0" encoding="utf-8" ?>



    <section name="MyConfigurationSectionHandler" type="DataContractConfiguration.MyConfigurationSectionHandler, DataContractConfiguration"/>


  <MyConfigurationSectionHandler xmlns:i="" xmlns="">











Now we can use the ConfigurationManager to get our configuration object that can also be send over the wire using WCF.

MyConfigurationSectionHandler section = (MyConfigurationSectionHandler)ConfigurationManager.GetSection("MyConfigurationSectionHandler");

I also add small application as an example you can download from here.

Enjoy 🙂


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>