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

5Yr*_*DBA 5 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>
Run Code Online (Sandbox Code Playgroud)

小智 9

试试这个以获得第一个端点

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


Tim*_*son 2

查看<system.serviceModel>MSDN 中的文档

你应该:

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