Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


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

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.

Getting Set Up
Now that you know what data to store, where to store it, and how it will be stored, you can start implementing the CMAB in your project. First, the CMAB must be compiled before you can add the necessary assembly references that you will need.

Once you have downloaded and installed CMAB, the default location for the code to compile it can be found in the C:\Program Files\Microsoft Application Blocks for .NET\Configuration Management\Code folder. There is a version for Visual Basic .NET in the VB folder and a C# version in the CS folder. Pick the language of your choice and select the appropriate Microsoft.ApplicationBlocks.ConfigurationManagment solution. You will also find three QuickStart sample solutions in the same folder.

Figure 1. Use the Add Reference screen to set up the assembly references to use the CMAB.
Once the solution is selected and opened in Visual Studio .NET, compile the solution and you're ready to go. The two core CMAB assemblies that need to be referenced in your application are the Microsoft.ApplicationBlocks.ConfigurationManagement.dll, and the Microsoft.ApplicationBlocks.ConfigurationManagement.Interfaces.dll (see Figure 1). These assemblies can be found in the output folder of the specific language solution you used to compile the CMAB. One important thing to note is that if you plan to use the SqlStorage Storage Provider, you must also add Microsoft.ApplicationBlocks.Data.dll as an assembly reference to your application project.

With the necessary assembly references added to your project, it is time to dig into the application configuration file and add some entries to it. In Figure 2, you can see an example of a configuration file. The two main things you will have to do are add two custom configuration declarations XML elements to the <configSections> element and create a new Custom Configuration Section XML element.

Figure 2. This Hashtable configuration example uses a separate XML file to store the configuration data and utilizes the refreshOnChange event.
If your application is a Windows application, make sure an app.config file already exists; for a Web application or Web service, the web.config file is already created for you.

At the top of the config file, look for an element named within the element. If the is not present in your application configuration file, add it in.

Next, add a child XML element to the <configSections> element. This element needs to be named <section>. The <section> element has two attributes—name and type. The name attribute contains the custom section name used to define the different configuration section handlers and which configuration storage provider to use with them. For now, you can make the value of the name attribute applicationConfigurationManagement.

Make the value of the type attribute:

<span class="pf">Microsoft.ApplicationBlocks.ConfigurationManagement. ConfigurationManagerSectionHandler, Microsoft.ApplicationBlocks.ConfigurationManagement, Version=,Culture=neutral,PublicKeyToken=null</span>

Author's Note: There are no spaces or breaks in the preceding code, and it all goes on one line.

Now that the applicationConfigurationManagement section element is created, the second <section> element to be created within the <configSections> must be added. The second element allows the CMAB to determine which configuration section handler to use. The second <section> XML element, like the first, has both a name and a type attribute.

For the purposes of this walk-through, use the CMAB built-in XML Hashtable Serializer Configuration Section Handler so the value attribute is:

Microsoft.ApplicationBlocks.ConfigurationManagement. XmlHashtableSectionHandler,Microsoft. ApplicationBlocks.ConfigurationManagement, Version=,Culture=neutral,PublicKeyToken=null

Again, the preceding code is all one line with no spaces or breaks. The name attribute is myConfig. See Table 1 for a listing of the Configuration Section Handlers (CSH) included with CMAB.

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