C# – Loading System.ServiceModel configuration section using ConfigurationManager

cconfigurationmanagernetwcfxml

Using C# .NET 3.5 and WCF, I'm trying to write out some of the WCF configuration in a client application (the name of the server the client is connecting to).

The obvious way is to use ConfigurationManager to load the configuration section and write out the data I need.

var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");

Appears to always return null.

var serviceModelSection = ConfigurationManager.GetSection("appSettings");

Works perfectly.

The configuration section is present in the App.config but for some reason ConfigurationManager refuses to load the system.ServiceModel section.

I want to avoid manually loading the xxx.exe.config file and using XPath but if I have to resort to that I will. Just seems like a bit of a hack.

Any suggestions?

Best Answer

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

// 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 ...

Appears to work well.