Configuring ASP.NET Application with System.Configuration

Sometime we need to hardcode values into our code, but this is not the best practice since we’ll have to recompile code every time we need to update our application with new hard coded value or changes to such a value. In other word hard coding is bad. .NET Framework addresses this issue with the new way of specifying values that we would hard coded otherwise. In .NET framework we have configuration files where we store values and can access them as need in an object oriented way. System.Configuration is the main name space that will be using to access these variables from configuration files. There are two main classes what sit right on the top. They are: Configuration and ConfigurationManager classes.

Configuration Properties and Methods

 

NameDescription
AppSettings Gets the AppSettingsSection object
ConnectionStrings Gets the ConnectionStrings
ContextInformation Gets the ContextInformation
FilePath Gets the physical path
GetSection Returns the specified ConfigurationSection
GetSectionGroup Returns the specified ConfigurationSectionGroup
HasFile Indicates whether a configuration file exists
NamespaceDeclared Gets or sets a value if it has an XML namespace
RootSectionGroup Gets the ConfigurationSectionGroup for this Configuration object
Save Writes the configuration settings
SaveAs Writes the configuration settings

ConfigurationManager Properties and Methods

 

NameDescription
AppSettings Gets the AppSettingsSection data
ConnectionStrings Gets the ConnectionStringsSection data
GetSection Retrieves a specified configuration section
OpenExeConfiguration Opens the specified client configuration
OpenMachineConfiguration Opens the Machine Configuration file
OpenMappedExeConfiguration Opens the specified client configuration
OpenMappedMachineConfiguration Opens the specified client configuration

//Open app. file
System.Configuration myConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

//Open machine file
ExeConfigurationFileMap MyMap = new ExeConfigurationFileMap();
MyMap.ExeConfigFilename = @"DBStringOneDemo.exe.config";
System.Configuration myConfiguration = ConfigurationManager.OpenMappedMachineConfiguration(MyMap);

ConfigurationUserLevel Enumeration

 

NameDescription
None Gets the System.Configuration.Configuration
PerUserRoaming Gets the roaming System.Configuration.Configuration
PerUserRoamingAndLocal Gets the local System.Configuration.Configuration

There are several common sense tasks that can be hard coded into configuration file and later on retrieved by the application in an object oriented way. They are:

  1. supportedRuntime – determines version of the application.
  2. developmentMode – manages GAC for Assembly install/uninstall via DEVPATH. It instructs the CLR to use DEVPATH.
  3. codeBase – specifies Assembly in the GAC that we want to use if multiple assemblies are installed in our GAC, we need to specify version and href inside this tag for location.
  4. connectionStrings and appSettings – our configuration string for access DB.
Setting up connection string within .config file is done this way

 

<connectionStrings>

    <clear/>

    <addname="AdventureWorksString"

        providerName="System.Data.SqlClient"

        connectionString="Data Source=localhost;Initial

        Catalog=AdventureWorksDB; Integrated Security=true"/>

</connectionString>
Retrieval of connection information is done this way.
String HelloWorldVariable = ConfigurationSettings.AppSettings["Fool"];

There is newer way to access AppSetting and it’s done like that
NameValueCollection MyAppSettings = ConfigurationManager.AppSettings;
Console.WriteLine(MyAppSettings["Fool"]);
Console.WriteLine(MyAppSettings[0]);

In addition AppSetting implement IEnumerable interface that allows having collection of items and traversing them easily.