views:

70

answers:

4

Hi every one, 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();
            }
        }
    }
A: 

See this question: How to create Endpoints from app.config file in WCF?

phsr
Not very related to my question though... Thank you!
Nam Gi VU
A: 
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;
    }
}
Christian Hayter
+1  A: 

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.

Matthew Abbott
+1  A: 

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;
   }
}
marc_s
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?
Nam Gi VU
@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.
marc_s
@marc_s: I have edit my question. Please have a look.
Nam Gi VU