Changing Configuration in External File Example

You can use an external configuration file to extend application configuration settings and to control whether state information is preserved when those settings change, by enabling or disabling the application restart functionality. This is controlled by the restartOnExternalChanges attribute, which is applied to different configuration file sections.

The following sections include the code to build a sample Web application with a custom section whose configSource attribute points to an external configuration file and whose restartOnExternalChanges attribute is initially set to true.

Running the Web application shows what happens to a global postback counter when changes are made to the external configuration file, and the provided code example demonstrates what happens when restartOnExternalChanges is set to true, set to the default value, and set to false, respectively.

The sample Web application must include this Global.asax page. The following code example defines the page with a global postback counter. The counter keeps track of the postback requests generated by refreshing the application's Default.aspx page (defined later in this topic).

// Code that runs on application startup.
void Application_Start(object sender, EventArgs e) 
        // This counter is set to 0 the first time 
        // the application starts and, thereafter, every 
        // time it restarts.
        int postBackCount = 0;
        HttpApplicationState appState = Application.Contents;
        appState.Add("postBackKey", postBackCount);

The Default.aspx page contains code that creates the custom section in the Web.config file, adds an element to the External.config file, and displays the postback counter value. Note that the page must contain a Label control to display the postback counter's current value. The following code example shows one possible way to define this control.

<asp:Label ID="ResultId" runat="server" style="font-weight:bold; color:Red;"/>

The following code example defines the Default.aspx page.

protected void Page_Load(object sender, EventArgs e)
        ExternalConfiguration extConfig = null;
        if (!IsPostBack)
            // Create an instance of the cusom section.
            extConfig =
                new ExternalConfiguration();

        int postBackCount =
           (int) HttpContext.Current.Application["postBackKey"];
        ResultId.Text = postBackCount.ToString();

        HttpContext.Current.Application["postBackKey"] =
            (int)HttpContext.Current.Application["postBackKey"] + 1;

The Web.config file defines the MyAppSettings custom section, as shown in the following code example. This example uses the standard type to process this section, but you can create your own type. For details, see ConfigurationElement and ConfigurationSection.

    type="System.Configuration.AppSettingsSection, System.Configuration, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
    requirePermission="false" />

The External.config file contains the details of the MyAppSettings custom section. Before you run the application for the first time, you must create this file manually in the application root directory. For more information, see the configSource attribute in General Attributes Inherited by Section Elements. For this example, make sure that your code contains the following empty section.

<MyAppSettings></ MyAppSettings>.

The ExternalConfiguration.dll file contains the code that updates the MyAppSettings custom section. You can place your source code in the App_Code directory of your application. The example will be compiled by ASP.NET when your application is requested. You can also compile the sample provider as a library and place it in the Bin directory of your Web application. The following code example shows how to compile the sample from the command line.

csc /out: ExternalConfiguration.dll /t:library ExternalConfiguration.cs /r:System.Web.dll /r:System.Configuration.dll

The following code example builds the ExternalConfiguration .dll file.

using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.Configuration;

namespace Samples.AspNet

    public class ExternalConfiguration

        // Instantiate the ExternalConfiguration type. 
        public ExternalConfiguration()
            // Access the root Web.config file.
            System.Configuration.Configuration config =

            // Get the custom MyAppSettings section.
            AppSettingsSection appSettings =

            // Perform the first update.

        // Update the custom MyAppSettings section. 
        // Note , if the section restartOnExternalChanges attribute  
        // is set to true, every update of the external  
        // configuration file will cause the application  
        // to restart. 
        public void UpdateAppSettings()

            System.Configuration.Configuration config =

            AppSettingsSection appSettings =

            int count = appSettings.Settings.Count;

                "key_" + count.ToString(), 
                "value_" + count.ToString());



The first time you run the application, it adds elements to the External.config file.

Every time you refresh the Default.aspx page, the postback counter is reset to zero. This is because the restartOnExternalChanges attribute in the MyAppSettings section is set to true, by default.

If, in the Web.config file, you set the restartOnExternalChanges attribute to false, you will notice that the postback counter keeps incrementing when you refresh the page, despite the changes in the External.config file. This is because you have disabled the application domain restart.