data access application error for app.config only Database db = DatabaseFactory.CreateDatabase(); is null

Nov 20, 2009 at 8:20 AM
I created a library for accessing SQL Server and i reference the Microsoft.Practices.EnterpriseLibrary.Data
dll that is not from GAC. I deleted the strong naming part like bellow. I fille the app.config with Ent Lib Tool
usually it works for web.config but not for app.config I have an application and using the DAL library
with app.config. It fails like bellow I do not get it why
at line Database db = DatabaseFactory.CreateDatabase();

    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
    <add name="Hallo" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=Halloween;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  <dataConfiguration defaultDatabase="Hallo" />


System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
       at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseMapper.MapName(String name, IConfigurationSource configSource)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
       at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
       at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
       at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp[TTypeToBuild](IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, IConfigurationSource configurationSource)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IConfigurationSource configurationSource)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.NameTypeFactoryBase`1.CreateDefault()
       at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase()
       at DataLayer.CategoriesDAL..ctor() in C:\Documents and Settings\geo\My Documents\Visual Studio 2005\Projects\FunStore\DataLayer\CategoriesDAL.cs:line 17
       at Server.ServerRemote.GetCategories() in C:\Documents and Settings\geo\My Documents\Visual Studio 2005\Projects\FunStore\Server\ServerRemote.cs:line 21
       at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
       at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
       at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)


All Replies

Nov 20, 2009 at 9:02 AM


Do you do this with remoting?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Nov 22, 2009 at 6:51 PM
Edited Nov 22, 2009 at 6:55 PM

Yes Sir.

I have a web application that calls a remote object and that remote object calls a Data Access Layer library (dll). I added to the DAL dll (library) an app.config file and I configured it with Ent Lib Config Tool, set the connection string and default database.

I noticed that the dll does not load the app.config by itself like a web app loads web.config. I modified the web app to call directly the DAL without remoting (on the same computer) and still the library cannot read from app.config. Of course I does not help set the configuration in web.config if the DAL and remote object is on other computer.

In all cases it fails at line 

db =

I guess because it cannot read the app.config file and the connection string

I guess when having a 3 layer architecture with application server you need a DAL with app.config file?

Thank you 


Nov 23, 2009 at 2:00 AM

It's because libraries don't own configuration files.  If a class from a library has codes that access the config file, it looks for the config file of the host project even if you have an app.config belonging to that library.  If you need that class library to read its own config, you would have to make use of a FileConfigurationSource -


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Nov 23, 2009 at 5:45 PM

Thank you Sarah

FileConfigurationSource can find the config file if it's deployed in the hosting app?

If I use remoting hosted in IIS then it needs to be in the virtual folder where the remote components are hosted

Is it a better architecture than that?

Thank you