简体   繁体   English

如何读取app.config中定义的属性的值?

[英]How to read value of an attribute defined in app.config?

I have a app.config file that in the form of : 我有一个app.config文件,其形式为:

<?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". 我想读取节点端点的属性“address”的值,其名称为“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). (继续讨论与marc_s讨论。抱歉把文本放在这里,因为评论不允许格式化代码)@marc_s:我使用下面的代码来读取上面的文件,但它显示clientSection.Endpoints有0个成员(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();
            }
        }
    }

You really don't need to - the WCF runtime will do all of that for you. 你真的不需要--WCF运行时将为你做所有这些。

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;
   }
}

You can use the ServiceModelSectionGroup (System.ServiceModel.Configuration) to access the configuration: 您可以使用ServiceModelSectionGroup(System.ServiceModel.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. 希望有所帮助。

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM