writing app.config bindings in c#

Hai experts.I need small conversion of app.config file.
below is the app.config code,,

 <client>
      <endpoint behaviorConfiguration="ClientCertificateBehavior"
                binding="wsHttpBinding"
                bindingConfiguration="wsHttpsBinding"
                address="https://sampleadad:8055/mainservice.svc/secure"
                contract="mainservicee"
                name="https" />
    </client>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpsBinding">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>


How can I convert this in c#.I mean c# code for the above
Config sections...which i will implement from my class file..
Thanks in advance..Needed urgently..
sandydv85Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lazyberezovskyCommented:
Example: http://msdn.microsoft.com/en-us/library/system.configuration.configurationelement.aspx
You should create custom configuration section and register in configSections.
Also I removed <wsHttpBinding> node from your xml.

App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="myApp" type="MyApp.MyAppConfigurationSection, MyApp"/>
  </configSections>
  <myApp>
    <client>
      <endpoint behaviorConfiguration="ClientCertificateBehavior"
                binding="wsHttpBinding"
                bindingConfiguration="wsHttpsBinding"
                address="https://sampleadad:8055/mainservice.svc/secure"
                contract="mainservicee"
                name="https" />
    </client>
    <bindings>
      <binding name="wsHttpsBinding">
        <security mode="Transport">
          <transport clientCredentialType="Certificate" />
        </security>
      </binding>
    </bindings>
  </myApp>
</configuration>

Usage (mind you need to add reference to System.Configuration):
MyAppConfigurationSection settings = (MyAppConfigurationSection)ConfigurationManager.GetSection("myApp");
Console.WriteLine(settings.Client.Endpoints["https"].Address);
Console.WriteLine(settings.Bindings["wsHttpsBinding"].Security.Transoport.ClientCredentialType);

Code:


namespace MyApp
{
    public class MyAppConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("client")]
        public ClientSettings Client
        {
            get { return (ClientSettings)base["client"]; }
        }

        [ConfigurationProperty("bindings")]
        [ConfigurationCollection(typeof(BindingSettings), AddItemName = "binding")]
        public BindingSettingsCollection Bindings
        {
            get { return (BindingSettingsCollection)base["bindings"]; }
        }
    }

    public class ClientSettings : ConfigurationElement
    {       
        // BTW from your xml exampl it's not clear whether it contains single endpoint element or collection.
        // If it is a single element, change code by yourself.
        private static readonly ConfigurationProperty _endpoints = new ConfigurationProperty(null, typeof(EndpointSettingsCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);

        [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
        [ConfigurationCollection(typeof(EndpointSettings), AddItemName = "endpoint")]
        public EndpointSettingsCollection Endpoints
        {
            get { return (EndpointSettingsCollection)base[_endpoints]; }
        }
    }

    public class EndpointSettingsCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new EndpointSettings();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((EndpointSettings)element).Name;
        }

        new public EndpointSettings this[string Name]
        {
            get { return (EndpointSettings)BaseGet(Name); }
        }
    }

    public class EndpointSettings : ConfigurationElement
    {
        // This attribute could be of enum type.
        [ConfigurationProperty("behaviorConfiguration")]
        public string BehaviorConfiguration
        {
            get { return (string)base["behaviorConfiguration"]; }
        }

        [ConfigurationProperty("binding")]
        public string BindingName
        {
            get { return (string)base["binding"]; }
        }

        [ConfigurationProperty("bindingConfiguration")]
        public string BindingConfiguration
        {
            get { return (string)base["bindingConfiguration"]; }
        }

        [ConfigurationProperty("address")]
        public string Address
        {
            get { return (string)base["address"]; }
        }

        [ConfigurationProperty("name")]
        public string Name
        {
            get { return (string)base["name"]; }
        }

        [ConfigurationProperty("contract")]
        public string Contract
        {
            get { return (string)base["contract"]; }
        }
    }

    public class BindingSettingsCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new BindingSettings();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((BindingSettings)element).Name;
        }

        new public BindingSettings this[string Name]
        {
            get { return (BindingSettings)BaseGet(Name); }
        }
    }

    public class BindingSettings : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get { return (string)base["name"]; }
        }

        [ConfigurationProperty("security")]
        public SecuritySettings Security
        {
            get { return (SecuritySettings)base["security"]; }
        }
    }

    public class SecuritySettings : ConfigurationElement
    {
        // This attribute could be of enum type.
        [ConfigurationProperty("mode")]
        public string Mode
        {
            get { return (string)base["mode"]; }
        }

        [ConfigurationProperty("transport")]
        public TransportSettings Transoport
        {
            get { return (TransportSettings)base["transport"]; }
        }
    }

    public class TransportSettings : ConfigurationElement
    {
        // This attribute could be of enum type.
        [ConfigurationProperty("clientCredentialType")]
        public string ClientCredentialType
        {
            get { return (string)base["clientCredentialType"]; }
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sandydv85Author Commented:

   Thanks lazyberezovsky: for u r elaborate reply..can i call proxy generated app.config
 file into another class library?Is so pls tell me how can I?
 
0
lazyberezovskyCommented:
Didn't get your new question.
If you are getting file from somewhere it's not the application configuration file - it's just usual xml file.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.