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

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

           

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

 List addresses = GetMyCurrentEndpoints(); 

结果我们会:

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

 // 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 endpointNames = new List(); foreach (ChannelEndpointElement endpointElement in endpointCollection) { endpointNames.Add(endpointElement.Name); } // use endpointNames somehow ... 

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

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

 private List GetMyCurrentEndpoints() { var endpointList = new List(); 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; }