Checking web.config on Application Start

During development, changes to configuration files are inevitable, such as adding connection strings or service bindings. When several programmers work in the same project, synching these changes manually is prone to errors, so you better have your application startup code check the settings are there.

Let’s start with the simple task: hooking the config file checker into the startup sequence in Global.asax.cs:

namespace My.Namespace
  public class Global: System.Web.HttpApplication

    protected void Application_Start(object sender, EventArgs e)

In the AppChecker class, we check whatever settings we want to make sure exists.

This example will check whether required service endpoints and bindings are present in the web.config file. Since the web.config is a valid XML file (otherwise app startup will fail with an exception), we can use the System.Xml namespace to load and parse it.

namespace My.Namespace
  public static class AppChecker
    public static void Check()
      Configuration cfg = WebConfigurationManager.OpenWebConfiguration(
      XmlDocument WebConfig = new XmlDocument();

      XmlNode ndSvc =
      if (ndSvc == null)
        throw new Exception(
          "web.config misses /configuration/system.serviceModel");

      CheckContract(ndSvc, "My.Namespace.ServiceClientProxy.IFooService");
      CheckContract(ndSvc, "My.Namespace.ServiceClientProxy.IBarProcess");

All service information is stored in the <system.serviceModel> section (variable ndSvc). In this section, we look up the required service proxy classes. Then we retrieve the binding from the proxy class. If either retrieval fails, the method will raise an exception.

    private static void CheckContract(XmlNode ndSvc, string Contract)
      XmlNode ndEndPoint = ndSvc.SelectSingleNode(
        "client/endpoint[@contract='" + Contract + "']");
      if (ndEndPoint == null)
        throw new Exception(
          "web.config does not contain endpoint for contract " + Contract);

      XmlNode ndBinding = ndSvc.SelectSingleNode(
        "bindings/basicHttpBinding/binding[@name='" +
        ndEndPoint.Attributes["bindingConfiguration"].Value + "']");
      if (ndBinding == null)
        throw new Exception("web.config does not contain binding " +
          ndEndPoint.Attributes["bindingConfiguration"].Value +
          " for endpoint " + ndEndPoint.Attributes["contract"].Value);

If your web application references the assemblies of your proxy classes directly, you may prefer the type-safe method:

    private static void CheckContract(XmlNode ndSvc, Type contract)
      CheckContract(ndSvc, contract.FullName);

One Response to Checking web.config on Application Start

  1. […] Checking web.config on Application Start sketched how to use XmlDocument and XPath to check required entries in the <serviceModel> section used by web service clients. […]

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: