The ConfigurationManager class allows you to open your own app.config (renamed to .exe.config) or web.config, but also the design-mode app.config of any other application by providing a ExeConfigurationFileMap parameter to the OpenMappedExeConfiguration method:![]()
var config =
ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap { ExeConfigFilename = cfg },
ConfigurationUserLevel.None);
You can then access the <host> configuration like this
ServiceModelSectionGroup serviceModel =
ServiceModelSectionGroup.GetSectionGroup(config);
if (serviceModel != null
&& serviceModel.Services != null
&& serviceModel.Services.Services != null)
{
foreach (ServiceElement service in serviceModel.Services.Services)
{
foreach (BaseAddressElement addr in service.Host.BaseAddresses)
Console.WriteLine(addr.BaseAddress);
}
}
This method works fine unless the services section has been extended by custom config sections such as a custom behavior
<extensions>
<behaviorExtensions>
<add name="myBehaviorExtension"
type="My.BehaviorExtension, My.BehaviorExtension.Library" />
</behaviorExtensions>
</extensions>
Such a custom extension without an accessible library implementing the type causes a ConfigurationErrorsException:
The type ‘x’ registered for extension ‘y’ could not be loaded
If this is the case, the only way I found was to load the .config file as an XmlDocument
XmlDocument xml = new XmlDocument(); xml.Load(cfg);
and process the results of the xml’s SelectNodes() method
foreach (XmlNode nd in xml.SelectNodes(
"/configuration/system.serviceModel/" +
"services/service/host/baseAddresses/add"))
{
var url = nd.Attributes["baseAddress"];
if (url != null)
Console.WriteLine(url.Value);
}

[...] extracts URLs of .asmx files and web service declarations from app.config. [...]