Как программно считать настройки WCF-клиента? - CodeHelper

Как программно считать настройки WCF-клиента?

1

Есть приложение — WCF-клиент. В его файле Web.config описаны точки подключения к сервисам WCF (endpoints):

<client>
  <endpoint 
    name="Service1" 
    address="http://localhost:3871/Service1.svc" 
    binding="basicHttpBinding" bindingConfiguration="commonBinding" 
    contract="IService1"/>
  <endpoint 
    name="Service2" 
    address="http://localhost:3871/Service2.svc" 
    binding="basicHttpBinding" bindingConfiguration="commonBinding" 
    contract="IService2"/>
  <!-- и т.д.-->
</client>

Можно ли программно в самом приложении считать эти настройки? В основном нужно получить используемые адреса, то есть атрибуты address элементов endpoint.

Лучший ответ:

2

В веб-приложении можно сделать так:

var clientSection = (ClientSection) WebConfigurationManager.GetSection("system.serviceModel/client");
var endpoints = clientSection.Endpoints;

Затем из коллекции endpoins можно достать всё, что нужно:

foreach (ChannelEndpointElement endpoint in Model)
{
    var address = endpoint.Address;
    var name = endpoint.Name;
    // ...
}
v1.7.123.556
© 2009—2010 CodeHelper FAQ | О сайте | Обратная связь | История изменений | Статьи
Creative Commons LicenseМатериалы сайта распространяются под лицензией Creative Commons Attribution-Share Alike 3.0 Unported.