Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I discover current endpoints of my c# application programmatically?

Tags:

c#

wcf

endpoints

How can I code a c# sample for reading my Client endpoint configurations:

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

And the goal is to obtain an array of endpoints address:

List<string> addresses = GetMyCurrentEndpoints();

As result we would have:

[0] http://mycoolserver/FinancialService.svc  
[1] http://mycoolserver/HumanResourcesService.svc
like image 843
Junior Mayhé Avatar asked May 23 '11 19:05

Junior Mayhé


1 Answers

This is my first answer ever. Be gentle :)

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;
}
like image 104
jglouie Avatar answered Nov 05 '22 19:11

jglouie