Как программно обнаружить текущие конечные точки моего приложения на c #? - PullRequest
11 голосов
/ 23 мая 2011

Как я могу написать пример c # для чтения конфигураций моей конечной точки клиента:

<client>
   <endpoint address="http://mycoolserver/FinancialService.svc"
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IFinancialService"
      contract="IFinancialService" name="WSHttpBinding_IFinancialService">
      <identity>
         <dns value="localhost" />
      </identity>
   </endpoint>
   <endpoint address="http://mycoolserver/HumanResourcesService.svc"
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHumanResourceService"
      contract="IHumanResourceService" name="WSHttpBinding_IHumanResourceService">
      <identity>
         <dns value="localhost" />
      </identity>
   </endpoint>

И цель состоит в том, чтобы получить массив адресов конечных точек:

List<string> addresses = GetMyCurrentEndpoints();

В результате мы получили бы:

[0] http://mycoolserver/FinancialService.svc  
[1] http://mycoolserver/HumanResourcesService.svc

Ответы [ 2 ]

17 голосов
/ 24 мая 2011

Это мой первый ответ. Будьте нежны:)

private List<string> GetMyCurrentEndpoints()
{
    var endpointList = new List<string>();

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    var serviceModel = ServiceModelSectionGroup.GetSectionGroup(config);

    foreach (ChannelEndpointElement endpoint in serviceModel.Client.Endpoints)
    {
        endpointList.Add(endpoint.Address.ToString());
    }

    return endpointList;
}
15 голосов
/ 23 мая 2011
// Automagically find all client endpoints defined in app.config
ClientSection clientSection = 
    ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;

ChannelEndpointElementCollection endpointCollection =
    clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;
List<string> endpointNames = new List<string>();
foreach (ChannelEndpointElement endpointElement in endpointCollection)
{
    endpointNames.Add(endpointElement.Name);
}
// use endpointNames somehow ...

(взято из http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html)

...