title: DatabaseFactory.CreateConnection() throws Object reference not set to an instance of an object
body: <p>I have a client application consuming a WCF service throws an Error</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>Details:</p>
  
  <p>I am using WCF service with Microsoft Enterprise library for database connectivity, the service runs fine in the self hosted environment but throws an error if hosted as a <code>windows service</code>. I am not able to debug the WCF service, since it is hosted as windows service, so i tried using try catch blocks to find out, where the problem is.</p>
  
  <p>I came across these two lines which i found to be most suspicious</p>
  
  <pre><code>Database db = DatabaseFactory.CreateDatabase();
  DbConnection conn = db.CreateConnection();
  </code></pre>
  
  <p>I have already set my default database in config file. i think the second line must be the cause.</p>
  
  <p>Please help.</p>
  
  <p>Tried to find similar question on stack overflow but failed, hence posted.</p>
  
  <blockquote>
    <p>Appended Question statement with error snap + stack trace...</p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/tdvqp.png" alt="enter image description here"></p>
  
  <p><strong>stack trace</strong></p>
  
  <p>at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseConfigurationView.get_DefaultName()</p>
  
  <p>at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseMapper.MapName(String name, IConfigurationSource configSource)</p>
  
  <p>at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)</p>
  
  <p>at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)</p>
  
  <p>at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)</p>
  
  <p>at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp[TTypeToBuild](IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies)</p>
  
  <p>at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, IConfigurationSource configurationSource)</p>
  
  <p>at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IConfigurationSource configurationSource)</p>
  
  <p>at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.NameTypeFactoryBase`1.CreateDefault()</p>
  
  <p>at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase()</p>
  
  <p>at WCFService.Service1.ExecuteSFDS(Cmd CmdObj, Int32 executionType) in D:\Projects\WCFService\WCFService\Service1.cs:line 32</p>