Editare

Partajați prin


ConfigurationBuilder Class

Definition

Represents the base class to be extended by custom configuration builder implementations.

public ref class ConfigurationBuilder abstract : System::Configuration::Provider::ProviderBase
public abstract class ConfigurationBuilder : System.Configuration.Provider.ProviderBase
type ConfigurationBuilder = class
    inherit ProviderBase
Public MustInherit Class ConfigurationBuilder
Inherits ProviderBase
Inheritance
ConfigurationBuilder

Examples

The following example shows how to implement a simple ConfigurationBuilder to read Environment variables:

using System;
using System.Configuration;
using System.Xml;

namespace Samples.AspNet.Config
{

   public class SampleConfigurationBuilder : ConfigurationBuilder
    {

        public override XmlNode ProcessRawXml(XmlNode rawXml) 
        {

            string rawXmlString = rawXml.OuterXml;

            if (String.IsNullOrEmpty(rawXmlString)) {
                return rawXml;
            }

            rawXmlString = Environment.ExpandEnvironmentVariables(rawXmlString);

            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.LoadXml(rawXmlString);
            return doc.DocumentElement;
        }

        public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection) 
            => configSection;
    }
}
Imports System.Configuration
Imports System.Xml

Public Class SampleConfigurationBuilder : Inherits ConfigurationBuilder

    Public Overrides Function ProcessRawXml(rawXml As XmlNode)  As XmlNode

        Dim rawXmlString As String = rawXml.OuterXml

        If String.IsNullOrEmpty(rawXmlString) Then
            Return rawXml
        End If

        rawXmlString = Environment.ExpandEnvironmentVariables(rawXmlString)

        Dim doc As New XmlDocument()
        doc.PreserveWhitespace = True
        doc.LoadXml(rawXmlString)
        Return doc.DocumentElement
    End Function

    Public Overrides Function ProcessConfigurationSection(configSection As ConfigurationSection)  As ConfigurationSection
       Return configSection
    End Function

End Class

The following example is an excerpt of the configuration file as it applies to the previous example. This applies environment variables to the appSettings configuration and makes those values available under ConfigurationManager.AppSettings.

<!-- To declare and use Configuration Builders in your configuration chain, update your app.config or web.config file as follows:  -->

<configSections>
  <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
</configSections>

<configBuilders>
  <builders>
    <add name="SampleConfigurationBuilder" type="CustomConfigBuilders.MyConfigBuilder, CustomConfigBuilders" />
  </builders>
</configBuilders>

<!-- To apply Configuration Builders to a configuration section, use the 'configBuilders' tag as follows:  -->
<appSettings configBuilders="SampleConfigurationBuilder">
  <add key="COMPUTERNAME" value="Will Be Replaced by EnvironmentVariable" />
</appSettings>

Remarks

Derive from this class to read configuration from an external source that you would like to consume in your .NET Framework application using the standard ConfigurationManager API. ConfigurationBuilders are available on NuGet.org to read from environment variables, Azure key vault, and a number of other sources.

Several implementations of ConfigurationBuilders are available from NuGet.org:

Constructors

ConfigurationBuilder()

Initializes a new instance of the ConfigurationBuilder class.

Properties

Description

Gets a brief, friendly description suitable for display in administrative tools or other user interfaces (UIs).

(Inherited from ProviderBase)
Name

Gets the friendly name used to refer to the provider during configuration.

(Inherited from ProviderBase)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
Initialize(String, NameValueCollection)

Initializes the configuration builder.

(Inherited from ProviderBase)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ProcessConfigurationSection(ConfigurationSection)

Accepts a ConfigurationSection object from the configuration system and returns a modified or new ConfigurationSection object for further use.

ProcessRawXml(XmlNode)

Accepts an XmlNode representing the raw configuration section from a config file and returns a modified or new XmlNode for further use.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to