A day with .Net

My day to day experince in .net

Adding assembly specific application configuration file

Posted by vivekcek on March 10, 2011

Hi friends vivek here.So you want to add a confifuration file to one of your assembly,and that assembly is used in an ASP.NET application.You cant achieve it directly. Because one aplication domain of ASP.NET support only one configuration file thats the web.config.But dont worry i have a solution for that.

1.Create an aseembly named Logger.
2.Add an application configuration file to this assembly name it as Logger.dll.config(The config file name should conatin your assembly name).
3.Wrte this tag in your configuraion file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <assemblySettings>
    <add key="OTA" value="Data Source=server\SQL2008;Initial Catalog=db;User ID=sdfx;Password=sdfs;Provider=SQLOLEDB"/>
  </assemblySettings>
</configuration>

4.Add a class named AssemblySettings.cs in your library with the below code.

using System;
using System.Reflection;
using System.Collections;
using System.Xml;
using System.Configuration;
using System.Runtime.CompilerServices;


namespace OTA.API.Logger
{
    internal class AssemblySettings
    {
        private IDictionary settings;

        [MethodImpl(MethodImplOptions.NoInlining)]
        public AssemblySettings()
            : this(Assembly.GetCallingAssembly())
        {
        }

        public AssemblySettings(Assembly asm)
        {
            settings = GetConfig(asm);
        }

        public string this[string key]
        {
            get
            {
                string settingValue = null;

                if (settings != null)
                {
                    settingValue = settings[key] as string;
                }

                return (settingValue == null ? "" : settingValue);
            }
        }

        public static IDictionary GetConfig()
        {
            return GetConfig(Assembly.GetCallingAssembly());
        }

        public static IDictionary GetConfig(Assembly asm)
        {
            // Open and parse configuration file for specified
            // assembly, returning collection to caller for future
            // use outside of this class.
            //
            try
            {
                string cfgFile = asm.CodeBase + ".config";
                const string nodeName = "assemblySettings";

                XmlDocument doc = new XmlDocument();
                doc.Load(new XmlTextReader(cfgFile));

                XmlNodeList nodes = doc.GetElementsByTagName(nodeName);

                foreach (XmlNode node in nodes)
                {
                    if (node.LocalName == nodeName)
                    {
                        DictionarySectionHandler handler = new DictionarySectionHandler();
                        return (IDictionary)handler.Create(null, null, node);
                    }
                }
            }
            catch
            {
            }

            return (null);
        }
    }
}

5.Create a class Log.cs from that class you can acess config file value as

AssemblySettings ASM = new AssemblySettings(System.Reflection.Assembly.GetExecutingAssembly());
String ConnectionString = ASM["OTA"].ToString(); 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s