Reading Web Service Definition from Other Program’s app.config or web.config

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 =
    new ExeConfigurationFileMap { ExeConfigFilename = cfg },

You can then access the <host> configuration like this

ServiceModelSectionGroup serviceModel =
if (serviceModel != null
    && serviceModel.Services != null
    && serviceModel.Services.Services != null)
  foreach (ServiceElement service in serviceModel.Services.Services)
    foreach (BaseAddressElement addr in service.Host.BaseAddresses)

This method works fine unless the services section has been extended by custom config sections such as a custom behavior

    <add name="myBehaviorExtension"
      type="My.BehaviorExtension, My.BehaviorExtension.Library" />

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();

and process the results of the xml’s SelectNodes() method

foreach (XmlNode nd in xml.SelectNodes(
  "/configuration/system.serviceModel/" +
  var url = nd.Attributes["baseAddress"];
  if (url != null)

1 thought on “Reading Web Service Definition from Other Program’s app.config or web.config

  1. Pingback: Analyzing Visual Studio Solutions « devioblog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.