如何从app.config获取此配置值?

时间:2010-07-09 16:00:04

标签: c#

我的朋友有以下app.config。他希望获得address的价值。怎么做?

<configuration>
    <system.serviceModel>
...
           <client>
            <endpoint address="http://ldo:8080/LLService" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_ILLService" contract="LLServiceReference.ILLService"
                name="WSHttpBinding_ILLService">
                <identity>
                    <userPrincipalName value="ggldoe@mail.com" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
...
</configuration>

2 个答案:

答案 0 :(得分:8)

尝试获取第一个端点

Configuration configuration =    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ServiceModelSectionGroup serviceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);
ClientSection clientSection = serviceModelSectionGroup.Client;
var el = clientSection.Endpoints[0];
return el.Address.ToString();

答案 1 :(得分:2)

查看<system.serviceModel> documentation in MSDN

你应该:

  1. 调用ServiceModelSectionGroup.GetSectionGroup方法
  2. serviceModelSectionGroup.Client.Endpoints集合中选择一个端点。大概你想看一个特定的合同。
  3. 查看该端点的Address属性