Browse DevX
Sign up for e-mail newsletters from DevX


Configure It Out with the Configuration Management Application Block : Page 5

Nearly every application needs some way to configure itself so that it can interact with a given environment. For the simplest applications, app.config or machine.config probably meet your needs. For more complex applications you probably need something a bit more robust, secure, and scalable—and that's when you need to explore the Configuration Management Application Block.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Reading Data
There are two overloaded Read methods. The first method accepts a string parameter that defines the section name you want to use. The section name defines which CSH and CSP will be used for retrieving the configuration data.

In Figure 2, the name attribute in the configSection and section elements are the same, thus passing in myConfig to the Read method will tell the configuration manager to use the XML Hashtable Configuration Section Handler, and the XML File Storage data provider for retrieving the configuration data.

public object GetAllSettings( string sectionName) { return ConfigurationManager.Read( sectionName); }

It is possible to have multiple section handlers and data providers used within the same application just by specifying them in the application configuration file.

The second overloaded Read method accepts no parameters, and it uses the defaultSection attribute of the applicationConfigurationManagement element to define which CSP and CSH to use. One very important thing to note is that the Read method that uses the defaultSection can only be used with Hashtables. If you want to use some other object other than a Hashtable, the Read method must be called with the sectionName parameter.

One very important thing to note is that the Read method that uses the defaultSection can only be used with Hashtables.

public object GetMySetting(string key) { Hashtable configData = ConfigurationManager.Read(); return configData[key]; }

Writing Data
Writing data to the CSP is just about as easy as reading it. There are two overloaded Write methods. The first overload accepts a section name and an object representing your configuration data and the second overload, using the defaultSection attribute, only accepts the object representing your configuration data. Again, the same Hashtable-only rule applies when using the Write method overload without a parameter as its counterpart Read method.

public void SaveAllSettings(string sectionName, object data) { ConfigurationManager.Write(sectionName, data); }

Caching Data
For almost every application, performance can be a concern. The CMAB offers a solution by providing in-memory caching functionality. The caching is really handy when dealing with settings that change rarely.

With some CSPs, such as the included XmlFileStorage provider, an event can be raised within the CMAB to refresh the in-memory cache from the CSP. To use the CMAB caching, a configCache element must be added to the configSection element in your application's .NET configuration file, and two attributes, enabled and refresh, must be set. The enabled attribute can be set to true or false, allowing caching to be turned on or off. The refresh attribute uses extended format notation.

The extended format consists of five values separated by spaces. These values can consist of an asterisk (*) to represent all possible numbers, a single number to indicate a single value, or a comma-separated list of numbers that can indicate multiple values. The five separate values are as follows, in this exact order: minutes, hours, day of month, month, day of week. Here are some examples of possible different settings

  • 1 * * * *: The cache expires every hour, at 1 minute past the hour.
  • 0,15,30,45 * * * *: The cache expires every 15 minutes for an hour.
  • 0 0,12 * * *: The cache expires at noon and at midnight every day.
  • 0 0 * * 1,2,3,4,5: The cache expires at midnight only on weekdays.
  • 0 12 5 * 0: The cache expires at noon on the first Sunday after the 5th day of every month.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date