Loading System.ServiceModel configuration section using ConfigurationManager Loading System.ServiceModel configuration section using ConfigurationManager xml xml

Loading System.ServiceModel configuration section using ConfigurationManager


http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html

// Automagically find all client endpoints defined in app.configClientSection 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 ...

Appears to work well.


The <system.serviceModel> element is for a configuration section group, not a section. You'll need to use System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup() to get the whole group.


This is what I was looking for thanks to @marxidad for the pointer.

    public static string GetServerName()    {        string serverName = "Unknown";        Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);        ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);        BindingsSection bindings = serviceModel.Bindings;        ChannelEndpointElementCollection endpoints = serviceModel.Client.Endpoints;        for(int i=0; i<endpoints.Count; i++)        {            ChannelEndpointElement endpointElement = endpoints[i];            if (endpointElement.Contract == "MyContractName")            {                serverName = endpointElement.Address.Host;            }        }        return serverName;    }