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

后端 未结 2 1255
离开以前
离开以前 2021-01-02 01:02

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


   

        
相关标签:
2条回答
  • 2021-01-02 01:11
    // 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)

    0 讨论(0)
  • 2021-01-02 01:16

    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;
    }
    
    0 讨论(0)
提交回复
热议问题