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

拜拜、爱过 提交于 2019-11-30 12:20:54
// 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 ...

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

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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!