如何以编程方式发现我的c#应用程序的当前端点?

Jun*_*r M 12 c# wcf endpoints

如何编写ac#sample以读取我的客户端端点配置:

<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>
Run Code Online (Sandbox Code Playgroud)

目标是获取一组端点地址:

List<string> addresses = GetMyCurrentEndpoints();
Run Code Online (Sandbox Code Playgroud)

结果我们会:

[0] http://mycoolserver/FinancialService.svc  
[1] http://mycoolserver/HumanResourcesService.svc
Run Code Online (Sandbox Code Playgroud)

jgl*_*uie 18

这是我的第一个回答.要温柔 :)

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;
}
Run Code Online (Sandbox Code Playgroud)


Dmi*_*try 16

// 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 ...
Run Code Online (Sandbox Code Playgroud)

(摘自http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html)