Wednesday, June 9, 2021

C# Configuration files, their types, purposes and locations

Types of configuration files

There are different types of configuration files. They are 
  • Machine configuration files
  • Application configuration files
  • Security configuration files
The .NET Framework, through configuration files, provides developers and administrators control and flexibility over the way applications run.

Importance of configuration files

Configuration files are XML files that can be edited when needed. An administrator or application developer can control which protected resources an application can access, which versions of assemblies an application will use, and where remote applications and objects are located. Developers can put settings in configuration files, eliminating the need to recompile an application every time a setting changes.

Note that configuration files are read-only files. What it means? It means that the managed code can use the classes in the System.Configuration namespace to read settings from the configuration files, but not to write settings to those files. An administrator or app developer can edit the configuration files as per the need.

Predefined Tag Attributes in Configuration files

Configuration files contain XML elements, which are logical data structures that set configuration information. Within a configuration file, we use tags to mark the beginning and end of an element. For example, the <runtime> element consists of <runtime>child elements</runtime>. An empty element would be written as <runtime/> or <runtime></runtime>.

As with all XML files, the syntax in configuration files is case-sensitive.

We specify configuration settings using predefined attributes, which are name/value pairs inside an element's start tag.

Machine configuration file

The machine configuration file, Machine.config, contains settings that apply to an entire computer. This file is located in the %runtime install path%\Config directory. Note that Machine.config contains configuration settings for machine-wide assembly binding, built-in remoting channels, and ASP.NET.

How configuration system searches configuration files
The configuration system first looks in the machine configuration file for the <appSettings> element and other configuration sections that a developer might define. It then looks in the application configuration file. To keep the machine configuration file manageable, it is best to put these settings in the application configuration file. However, putting the settings in the machine configuration file can make your system more maintainable. For example, if we have a third-party component that both the client and server application uses, it is easier to put the settings for that component in one place. In this case, the machine configuration file is the appropriate place for the settings, so we don't have the same settings in two different files.

Application Configuration Files

An application configuration file contains settings that are specific to an app. This file includes configuration settings that the common language runtime reads (such as assembly binding policy, remoting objects, and so on), and settings that the app can read.

The name and location of the application configuration file depend on the app's host, which can be one of the following:
  • Executable–hosted app.
  • ASP.NET-hosted app.
  • Internet Explorer-hosted app.

I. Executable–hosted app: These apps have two configuration files: a source configuration file, which is modified by the developer during development, and an output file that is distributed with the app.
  • Source configuration file
  • Output configuration file
When developers develop in Visual Studio, they place the source configuration file for the app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called MyApp.exe should have a source configuration file called MyApp.exe.config.

Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app. 

II. ASP.NET-hosted app: ASP.NET configuration files are XML files. The .NET Framework defines a set of elements that implement configuration settings, and the ASP.NET configuration schema contains elements that control how ASP.NET Web applications behave.

Default configuration settings are specified in the Machine.config file located in the %SystemRoot%\Microsoft.NET\Framework\versionNumber\CONFIG\ directory. Values are inherited by child sites and applications. If there is a configuration file in a child site or application, the inherited values do not appear, but can be overridden and are available to the configuration API. The ASP.NET configuration schema elements that can be configured in found in the Machine.config files and in application-specific Web.config files. 

Note that ASP.NET configuration applies only to ASP.NET resources. The System.Web.Configuration namespace contains numerous classes that are used to set up ASP.NET configuration.

Basic structure of a configuration file

At the top of the configuration file is written the version of XML.
<?xml version="1.0"?>
Then <configuration> is the root element in every configuration file used by the common language runtime and .NET Framework applications.
The <configuration> tag has <configSections> tag as child tag which includes one or more <section> tags within <sectionGroup>.

Example:


<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
<appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

<system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

NOTE <appSettings> contains an application specific configuration, key-value pairs. <system.webServer> is webserver related configuration.

No comments:

Post a Comment

Hot Topics