Partager via


Sharing Configuration Sections across Multiple Applications

You can use redirection to read the configuration for individual application blocks (individual configuration sections) from a shared location or other configuration source. This enables you to use a local application configuration for some application blocks, while reading and including the shared sections configuration. You create composite configurations like this by specifying additional sources in your application configuration file. The contents of the individual sections in the configuration source can be read and included with the contents of your default application configuration file at run time.

The following procedure explains how you can use the configuration tools to redirect Enterprise Library to read a specific configuration section's content from a specified source.

To use redirection to read a configuration section from a shared source

  1. On the taskbar, click Start, point to All Programs, point to Microsoft patterns & practices, point to Enterprise Library 5.0, point to Enterprise Library Configuration, and then select the version of the configuration editor you require. Alternatively, right-click on a configuration file in Visual Studio Solution Explorer and click Edit Enterprise Library V5 Configuration.

  2. On the Blocks menu click Add Configuration Settings.

  3. In the Sources pane click the plus-sign icon, point to Add Sources and then click the type of configuration source you want to add to the configuration. This is the configuration source to which you will redirect configuration sections. In this example, we add a File-based Configuration Source.

    Ff664751.3f3f1ab9-4d22-4333-b89f-cfb6534f82be(en-us,PandP.50).png

  4. In the Sources column, enter the desired File Path property for the new configuration source by typing the path and file name or by clicking the ellipses (...) button and navigating to the file.

    Ff664751.0a3d6210-5f3e-4c58-a5ec-fd9ba1c78730(en-us,PandP.50).png

  5. You can now redirect sections from your local source to another using the Redirected Sections pane. Click the plus sign icon in the Redirected Sections column and click Add Redirected Section.

  6. In the new Redirected Section that is displayed, select the configuration section that you want to redirect to a shared configuration source. After you select the section, the name of the redirected section changes to reflect this.

    Ff664751.bf2d521d-6752-43b7-88e8-a9383d326227(en-us,PandP.50).png

  7. Select the configuration source that will expose the shared configuration section you want to use in the drop-down list for the Configuration Source property of the redirected section. A connecting line appears showing which configuration source will provide the information for the redirected section.

    Ff664751.5bdd7364-219e-4d76-9536-3e8c0b2e27bf(en-us,PandP.50).png

  8. Click Save or Save As to save the configuration.

Note

In the Configuration Tool, when redirected sections are used with a Selected Source other than System Configuration Source, the redirected sections configuration information will still be saved to the System Configuration Source, which is the file opened in the configuration tool. To edit the contents of the shared configuration sections, open the configuration that contains them directly.

An Example of the XML Generated by Redirected Sections

The following XML fragment specifies a system configuration source with a redirect for the Logging Application Block section.

<enterpriseLibrary.ConfigurationSource 
    selectedSource="System Configuration Source">

  <sources>
    <add name="System Configuration Source"
         type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, 
               Microsoft.Practices.EnterpriseLibrary.Common" />
    <add name="File-based Configuration Source" 
         type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource,
               Microsoft.Practices.EnterpriseLibrary.Common"
         filePath="c:\config\myapp.config" />
  </sources>

  <redirectSections>
    <add sourceName="File-based Configuration Source"
         name="loggingConfiguration" />
  </redirectSections>

</enterpriseLibrary.ConfigurationSource>