3.5 Reading Configuration Information


We have already seen how to retrieve values stored in the AppSettings element by calling ConfigurationSettings.AppSettings("xxx") , where xxx is the key we used to index the value. This static indexer is actually a convenience wrapper around a more general way of retrieving any configuration setting element through the ConfigurationSettings.GetConfig() method, as shown in Listing 3-9.

Listing 3-9 Reading Configuration Settings
 Dim settings As Object = _             ConfigurationSettings.GetConfig("appSettings") Dim nvc As NameValueCollection nvc = CType(settings, NameValueCollection) If Not nvc Is Nothing Then   Dim val As String = CStr(nvc("xxx")) End If 

Any configuration element (except the processModel element) can be retrieved using this technique. Internally, this maps into a request for the cached settings of the configuration file for that element, and if they have not yet been cached, a request to read them. At the lowest level of the configuration hierarchy, the physical XML files on disk are read by a class called ConfigurationRecord , using the XmlTextReader class to efficiently pull in the configuration data. Configuration information is parsed and stored on demand, so if a particular configuration element is never requested , it will never be read and loaded into memory. The job of parsing individual sections of the configuration file falls to configuration section handlers.

Conceptually, all ASP.NET configuration files are divided into two sections: the configuration section handlers and the configuration data. Until now, we have been adding information to the configuration data section only and have not even included the configuration section handler portion in our web.config files. If you look at the machine.config file on your system, however, you will see that the top of the file includes a number of configuration section handlers.

The job of a configuration section handler is to parse a portion of the configuration file, which makes this configuration file format extremely extensible. The classes responsible for reading in portions of the file are not established until the file is actually read, at which point instances of each parser object are created and passed the portion of the file they are responsible for. Figure 3-4 shows this relationship in a portion of the systemwide machine.config file. Note that the compilation section of the configuration file is parsed by a class called CompilationConfigurationHandler , specified under the configSections element. It also falls under a sectionGroup element, which further scopes the name of the section it will parse ”in this case, to be under an element named system.web . The other configuration section handler shown in this sample is the one for appSettings . Notice that this element is parsed by a class called NameValueFileSectionHandler and is a top-level element in the configuration file.

Figure 3-4. Configuration Section Handlers in machine.config

graphics/03fig04.gif

Each class added in the configSections element must implement the IConfigurationSectionHandler interface, shown in Listing 3-10, which has a single method called Create . The Create method is called by the top-level configuration file parser (the ConfigurationRecord class) when a tag designated for that handler is read in the configuration file. When the Create method is called for a handler, it is passed the parent configuration section (if there is one), the current HttpConfigurationContext object (through the input parameter), and most importantly, a reference to the XmlNode to be parsed by the handler. In most cases, the handler simply walks through the child nodes and attributes of the XmlNode passed in. The return value from this function is an object containing whatever state the handler wishes to retain in the running program, and it is cached and available anywhere in the application through the ConfigurationSettings.GetConfig() method.

Listing 3-10 IConfigurationSectionHandler Interface
 Public Interface IConfigurationSectionHandler   Function Create(parent As Object, input As Object, _                   node As XmlNode) As Object End Interface 

Most of the ASP.NET configuration section handlers define a corresponding state retainer class that acts as a repository for the configuration settings associated with that handler. For example, a CompilerConfiguration class stores the contents of the compilation element, a PagesConfiguration class stores the contents of the pages element, and so on. Thus, for any active ASP.NET application, there are a number of in-memory instances of configuration classes populated with configuration state. These instances are stored in a hash table that is global to the application, and all calls to the ConfigurationSettings.GetConfig() method are simply accesses into this global hash table, as shown in Figure 3-5.

Figure 3-5. In-memory Configuration Settings Layout

graphics/03fig05.gif

Be aware that most of the configuration classes used by ASP.NET are internal classes and are thus not accessible. ASP.NET uses the configuration classes to set the defaults and other values in the classes it creates.



Essential ASP.NET with Examples in Visual Basic .NET
Essential ASP.NET with Examples in Visual Basic .NET
ISBN: 0201760398
EAN: 2147483647
Year: 2003
Pages: 94
Authors: Fritz Onion

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net