简体   繁体   中英

how to read endpoint address value from app.config

I have asp.net web application and have the following in my app.config file.

<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?

I have come accross the this solution but it is not working for me asking for exe file path. Not sure which exe path file?

     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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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