Enterprise Library Class Library App.Config

Topics: Enterprise Library Core
Jul 10, 2012 at 6:42 AM
Edited Jul 10, 2012 at 6:44 AM

I have a class library whcih is a Wrapper class for Enterprise Library Logging Application Block. There I kept an App.Config configuration file. Where all the Enterprise Library related configurations are present.

Now I refered this Class Library in a ASP.NET Web Application. Problem I am getting is: The ASP.NET Web Application is unable to read the App.Config file to fetch Configuration Information. And I am getting below Exception:

 

 

 

1) The wrapper contains logging and execption block both.

2) I am using Ent Lib 5.0

3) I want to make a class library and distribute it to Webservice team and Web Server Team (UI). So I do not want to provide web.config configuration along with the dll.

4) Again I dont want to write configuration programatically...

Is there any other work around? I see when I refer this DLL in the web application then in the bin I can see the App.config file but the web application is unable to read this file... It is reading the web.config file instead

 

Jul 11, 2012 at 4:19 AM

If you want "automatic" loading of configuration you need to hook into either app.config or web.config.  Since you don't want to modify existing configuration for your scenario I would recommend using configuration but manually loading the configuration required for your component.

Here's what to do:

  1. Add App.Config to your library project (e.g. MyClassLibrary).  This will get compiled to MyClassLibrary.dll.config.
  2. Deploy MyClassLibrary.dll.config along with you class library.
  3. Write code to load your configuration and use the objects in your wrapper classes that you need.

The code would look something like this:

    public class MyLogger
    {
        private static LogWriter logWriter;

        static MyLogger()
        {
            FileConfigurationSource config = new FileConfigurationSource("MyClassLibrary.dll.config");
            IServiceLocator container = EnterpriseLibraryContainer.CreateDefaultContainer(config);
            logWriter = container.GetInstance<LogWriter>();
        }

        public void Write(string message)
        {            
            logWriter.Write(message, "General");
        }

    }

This is a naive implementation so, depending on your design, you may want to create a proper singleton or maintain a reference to the container or Dispose of any Disposable instance objects, etc.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com