简体   繁体   English

如何从app.config中读取端点地址值

[英]how to read endpoint address value from app.config

I have asp.net web application and have the following in my app.config file. 我有asp.net web application并且我的app.config文件中包含以下内容。

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="Test" />
    <binding name="Test1">
      <security mode="Test" />
    </binding>
    <binding name="BasicHttpBinding_IService1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://test:90001"
    binding="basicHttpBinding" bindingConfiguration="Test"
    contract="Test" name="HTTP_Port" />
     </client>
</system.serviceModel>

How i can get/read endpoint address value? 我如何get/read endpoint address值?

I have come accross the this solution but it is not working for me asking for exe file path. 我遇到了这个解决方案,但它不适用于我要求exe文件路径。 Not sure which exe path file? 不确定哪个exe路径文件?

     var serviceModel = ServiceModelSectionGroup.GetSectionGroup(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
        var endpoints = serviceModel.Client.Endpoints;
        foreach (ChannelEndpointElement e in endpoints)
        {
            if (e.Name == "HTTP_Port")
            Console.WriteLine(e.Address);
        }
        Console.ReadLine();

To read endpoints from appconfig you need to add reference of System.ServiceModel.Configuration and use GetSectionGroup method which return the serviceModel group and you can access all the endpoints in that section,if you just need the one you can access specific with the endpoint name. 要从appconfig中读取端点,您需要添加System.ServiceModel.Configuration的引用,并使用GetSectionGroup方法返回serviceModel组,并且您可以访问该部分中的所有端点,如果只需要一个可以使用端点名称访问的端点。

Also in the appconfig you provided the port is invalid, I got and error while reading the url with that port I assume you just added that as an example. 同样在appconfig中,您提供的端口无效,在读取带有该端口的url时出现错误,我假设您只是添加了该示例。

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

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