ASP.NET Configuration

by Douglas Reilly



Listing One



<appSettings>

    <add key="ApplicationName" value="DDJ ASP.NET Configuration" />

    <add key="Facility" value="DDJ" />

    <add key="ShortAppName" value="Hospital List" />

    <add key="CnStr" 

value="Server=xeon;database=ConfigurationDb;User ID=User;Pwd=password" />

</appSettings>





Listing Two



CREATE TABLE [dbo].[ConfigurationInfo] (

    [Key] [nvarchar] (50) NOT NULL ,

    [Value] [nvarchar] (255) NOT NULL 

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[ConfigurationInfo] WITH NOCHECK ADD 

    CONSTRAINT [PK_ConfigurationInfo] PRIMARY KEY  CLUSTERED 

    (

        [Key]

    )  ON [PRIMARY] 

GO







Listing Three



SqlConnection cn=new 

SqlConnection("Server=xeon;database=ConfigurationDb;

                                      User ID=User;Pwd=password");

cn.Open();

try

{

    SqlCommand cmd=new SqlCommand("SELECT [key],[value] 

                                        FROM ConfigurationInfo",cn);

    SqlDataAdapter da=new SqlDataAdapter(cmd);

    DataSet ds=new DataSet();

    da.Fill(ds,"ConfigruationInfo");



    ds.WriteXml("c:\\work\\Default.Config");

}

finally

{

    cn.Close();

}





Listing Four



<?xml version="1.0" standalone="yes"?>

<Configuration>

  <ConfigruationInfo>

    <key>Key</key>

    <value>Yet Another different Value from Default</value>

  </ConfigruationInfo>

  <ConfigruationInfo>

    <key>Key2</key>

    <value>Value2</value>

  </ConfigruationInfo>

</Configuration>



Listing Five



using System;

using System.Data;

using System.IO;

using System.Web.Caching;

using System.Web;

using System.Xml;



namespace ConfigurationLib

{

    /// <summary>

    /// Summary description for Class1.

    /// </summary>

    public class ConfigurationClass

    {

        private System.Web.HttpContext m_Context;   

        private bool m_HitFile;

        private const string c_CacheKeyName="ConfigurationCache_DDJ";

        

        public bool HitFile

        {

            get { return this.m_HitFile; }

        }

        public ConfigurationClass(System.Web.HttpContext CurrentContext)

        {

            this.m_HitFile=true;

            this.m_Context=CurrentContext;

        }

        public string GetValue(string key)

        {

            string ret=string.Empty;

            string FilterExpression=string.Format("Key='{0}'",key);

            DataSet ds=this.GetConfigDataset();

            DataRow[] dr=ds.Tables[0].Select(FilterExpression);

            if ( dr.Length>0 )

            {

                ret=dr[0]["Value"].ToString();

            }

            return ret;

        }

        private DataSet ConfigToDs(string ConfigurationFilename)

        {

            DataSet ds=new DataSet();

            try

            {

                ds.ReadXml(ConfigurationFilename);

                this.m_Context.Cache.Insert(ConfigurationClass.c_CacheKeyName,

                    ds,new CacheDependency(ConfigurationFilename));

            }

            catch

            {

                // Ignore...

            }

            return ds;

        }

        private DataSet GetConfigDataset()

        {

            DataSet ds=null;

            this.m_HitFile=false;

            ds=(DataSet)m_Context.Cache[c_CacheKeyName];

            if ( (ds==null) )

            {

                this.m_HitFile=true;

                string MachineName=System.Environment.MachineName;

                string ConfigurationFileName=

                    m_Context.Server.MapPath(MachineName + ".Config");

                

                if ( File.Exists(ConfigurationFileName) )

                {

                    ds=ConfigToDs(ConfigurationFileName);

                }

                else

                {

                    ConfigurationFileName=

                              m_Context.Server.MapPath("Default.Config");

                    if ( File.Exists(ConfigurationFileName) )

                    {

                        ds=ConfigToDs(ConfigurationFileName);

                    }

                }

            }

           return ds;

        }

    }

}





Listing Six



ds=(DataSet)m_Context.Cache[c_CacheKeyName];

if ( (ds==null) )

{ // Refill the cache... }

// Use the Cache element





Listing Seven



// INCORRECT CODE!

if ( (m_Context.Cache[c_CacheKeyName]==null) )

{ // Refill the cache... }

ds=(DataSet)m_Context.Cache[c_CacheKeyName];

// Use the Cache element





Listing Eight



string MachineName=System.Environment.MachineName;

string ConfigurationFileName=

    m_Context.Server.MapPath(MachineName + ".Config");

if ( File.Exists(ConfigurationFileName) )

{

    ds=ConfigToDs(ConfigurationFileName);

}

else

{

    ConfigurationFileName=m_Context.Server.MapPath("Default.Config");

    if ( File.Exists(ConfigurationFileName) )

    {

        ds=ConfigToDs(ConfigurationFileName);

    }

}





Listing Nine



ConfigurationClass c=new ConfigurationClass(HttpContext.Current);

string Value=c.GetValue("Key");

Response.Write("Key=" + Value + " Hit file? " + c.HitFile.ToString());

Value=c.GetValue("Key2");

Response.Write("<br>Key2=" + Value + " Hit file? " + c.HitFile.ToString());















1



