How to read value of an attribute defined in app.config?
Asked Answered
F

3

6

I have a app.config file that in the form of :

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <system.serviceModel>
      <client>
        <endpoint address="http://something.com"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer"
        contract="ABC" name="XXX" />
        <endpoint address="http://something2.com"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer"
        contract="ABC2" name="YYY" />
      </client>
    </system.serviceModel>
  </configuration>

I want to read the value at attribute "address" of node endpoint which has the name="XXX". Please show me how to do it!

(Continue belowing disscussing with marc_s. Sorry to put the text here since comment do not allow to format codes ) @marc_s: I use the below codes to read the above file but it shows that the clientSection.Endpoints has 0 members (Count=0). Please help!

public MainWindow()
    {
        var exeFile = Environment.GetCommandLineArgs()[0];
        var configFile = String.Format("{0}.config", exeFile);
        var config = ConfigurationManager.OpenExeConfiguration(configFile);
        var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config);
        var clientSection = wcfSection.Client;
        foreach (ChannelEndpointElement endpointElement in clientSection.Endpoints)
        {
            if (endpointElement.Name == "XXX")
            {
                var addr = endpointElement.Address.ToString();
            }
        }
    }
Frailty answered 30/6, 2010 at 11:45 Comment(4)
you shouldn't be using a *vshost.config" file - that's only going to be present if you run your program from within Visual Studio. Use MyApp.exe.config instead !!Helio
@marc_s: Hi, I have updated my code. Though, the number of endpoints read are still zero.Frailty
yes, I can see that - with my code it works, though. Try it! Don't use your ServiceModelSectionGroup.GetSectionGroup(config); and the following lines, but use my code instead (ConfigurationManager.GetSection(....)) - works in my case!Helio
@marc_s: I tried your code and it works! Thank you very much!Frailty
H
15

You really don't need to - the WCF runtime will do all of that for you.

If you really must - for whatever reason - you can do this:

using System.Configuration;
using System.ServiceModel.Configuration;

ClientSection clientSettings = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;

string address = null;

foreach(ChannelEndpointElement endpoint in clientSettings.Endpoints)
{
   if(endpoint.Name == "XXX")
   {
      address = endpoint.Address.ToString();
      break;
   }
}
Helio answered 30/6, 2010 at 12:2 Comment(2)
How do we define class ClientSection to use in your code? I'm not sure how to define Endpoints properties. Can we use ArrayList there?Frailty
@Nam Gi VU: ClientSection is defined in System.ServiceModel.Configuration, and it has an Endpoints collection. You're not defining anything here - you're just using existing framework classes.Helio
L
3

You can use the ServiceModelSectionGroup (System.ServiceModel.Configuration) to access the configuration:

    var config = ConfigurationManager.GetSection("system.serviceModel") as ServiceModelSectionGroup;
    foreach (ChannelEndpointElement endpoint in config.Client.Endpoints)
    {
        Uri address = endpoint.Address;
        // Do something here
    }

Hope that helps.

Lisalisabet answered 30/6, 2010 at 11:56 Comment(0)
M
0
var config = ConfigurationManager.OpenExeConfiguration("MyApp.exe.config");
var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config);
var clientSection = wcfSection.Client;
foreach(ChannelEndpointElement endpointElement in clientSection.Endpoints) {
    if(endpointElement.Name == "XXX") {
        return endpointElement.Address;
    }
}
Mollie answered 30/6, 2010 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.