Introduction
All the web applications needs
some data that is to be available for the whole application. Usually this data
is put in the web.config file. Sometimes writing information in the
web.config file is tricky since its an xml file and writing information
manually can be dangerous and may contain errors. Microsoft introduces "Configuration
Application Block" which is a part of the Enterprise Library.
Enterprise Library is like a tree which has many branches. Each branch can be
taken as a separate block which makes up the whole tree.
In this article we will see how
Configuration Application block can be used to keep information available to
all the users and which is used to configure the way the application works.
Adding References to the
libraries:
The first step that you should take is to
create references to the Configuration Library. If you choose the default
installation options than the file path to the Configuration Library can be
found at:
C:\Program Files\
Enterprise Library\ code\ bin\ Configuration.dll
Just refer to this library in your application.
After referencing this library you need to include this in your code using the
following syntax:
using
Microsoft.Practices.EnterpriseLibrary.Configuration;
Okay so now you have referenced to the
Configuration Application Block in your application.
Writing Information in the Configuration
Application Block:
If you manually want to write the information
in the configuration application block. Then good luck, because it has all the
xml tags which can easily produce error because you may miss some of the tags or
the attributes. For this reason Microsoft has provided a very cool Desktop Tool
that you can use to write the information in the Configuration File.
Just go to start
Select Program Files
Microsoft Patterns and Practices
Enterprise Library
and finally "Enterprise Library
Configuration".
Below is the "Enterprise library Configuration"
application which you will see:

Now click on the File and than open
application. Select the "Web.config" of your current web application.
This will make an application node. Right click on the application node and
select "Add new Configuration Block".

Now you have added a Configuration Block. You
need to add the Configuration Sections which will contain the information which
will be available to the application. Just right click on the Configuration
Block and select "Add Configuration Section".

The default name of the Configuration Section
is "Configuration Section". You can always change this name to the name that you
like. Let's say that we are storing information about "Color" of the website.
Let's change the name of the "Configuration
Settings" to "ColorSettings". Now right click on the ColorSettings
and add "Xml File Storage Provider" and "Xml Serializer transformer".
Here is the image below:

Now click on the "Save" button which will save
the information to the "Web.config" file and it also creates a new file
which is "colorConfig".
The colorConfig will contain the information
which you will write in it. Now if you view the "Web.config" file it will
contain a entry for the "ColorSettings".

Now let's see how we can write information in
the config file. Let's make a class which will be written in the Configuration
File. Here is a simple class for writing the color information:
public
class ColorClass
{
private
string name;
public
string Name
{
get {
return name; }
set {
name = value; }
}
public
ColorClass()
{
}
} |
Writing Configuration Information to the
config file:
Now let's see how we can write information in
the configuration file. Here is the simple button click code which is used to
write the information to the configuration file. The ConfigurationManager
class is the most important class of the Configuration block which contains all
the methods that you will need to write the information. In this example we used
WriteConfiguration method which is used to write the information to the
configuration file.
private
void Button1_Click(object
sender, System.EventArgs e)
{ColorClass color =
new
ColorClass();
color.Name = "Red";
ConfigurationManager.WriteConfiguration("ColorSettings",color);
} |
Explanation of the Code:
1) First we made an object of the class
ColorClass which we have defined earlier.
2) Next we used the "Name" property of the
Color class and assign it the string value "Red".
3) Finally we used the ConfigurationManager
class of the Configuration namespace and we used the WriteConfiguration method
to write the information in the configuration file.
After this method is executed you will see that
the configuration file has changed. We can also see below in the image that the
value of the Name is written in the Configuration file. Please note that this
configuration file is the colorconfig.config file and not the web.config file.

Now lets see how we can retrieve the
information from the configuration file. Reading the information in the
configuration file is also pretty straight- forwad and you can achieve this
using casting.
Here is the button click code to Read the
Configuration Information in the colorconfig.config file:
private
void Button2_Click(object
sender, System.EventArgs e)
{
ColorClass color = (ColorClass)
ConfigurationManager.GetConfiguration("ColorSettings");
Response.Write(color.Name);
} |
Explanation of the Code:
1) In the button click event we just made the
object of the ColorClass and used the GetConfiguration method of the
ConfigurationManager class to get the configuration information. Important point
to note is that we are also casting it to the (ColorClass) so it can be assigned
to the instance of the ColorClass.
2) Later we just display the color on the
screen and "red" will be displayed.
When ever you write something in the
Configuration file and go back to the Visual Studio it will let you know that
the file has been changed and do you want to reload the new file or not. You can
also find out the alteration of the file using the events available in the
ConfigurationManager class.
This event is fired when the Configuration
settings in the configuration file has been changed.
|
ConfigurationManager.ConfigurationChanged+=new
ConfigurationChangedEventHandler (ConfigurationManager_ConfigurationChanged); |
In this article we saw that how we can use
the Configuration Application Block of the Enterprise Library and benefit our
application. You can save all sort of configuration information in the
configuration file. This can include the font data for the whole application.