Issues using a separate EntLib.config file.

Topics: Data Access Application Block, Logging Application Block
Apr 19, 2012 at 9:00 PM
Edited Apr 19, 2012 at 9:13 PM

I've currently set up my project with Web.config and EntLib.config.  Web.config has the following:

  <configSections>
    <section name="enterpriseLibrary.ConfigurationSource"
             type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             requirePermission="true" />
    <section name="dataConfiguration"
             type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             requirePermission="true" />
    <section name="unity"
             type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
    <sources>
      <add name="System Configuration Source"
           type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      <add name="EntLib Configuration Source"
           type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           filePath="entlib.config" />
    </sources>
    <redirectSections>
      <add sourceName="EntLib Configuration Source"
           name="loggingConfiguration" />
    </redirectSections>
  </enterpriseLibrary.ConfigurationSource>

  <connectionStrings>
    <add name="LoggingDB Connection String"
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=LoggingDB;Integrated Security=SSPI;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <dataConfiguration defaultDatabase="LoggingDB Connection String" />

and EntLib.config has the rest.  Set up like this, I get errors in EnterpriseLibrary.Config:

Element  - Database Settings  
Property - Default Database Instance  
Message  - No Default Database Instance is specified in the Database Settings                                  
Severity - Warning

Element  - DBListener         
Property - Database Instance          
Message  - Reference item is not available.  Select an existing item or add a new item to Database Instances.  
Severity - Warning

Element  - DBTraceListener    
Property - Database Instance          
Message  - Reference item is not available.  Select an existing item or add a new item to Database Instances.  
Severity - Warning

If I move the dataConfiguration section to EntLib.config, the errors go away but when I try to run the app, I get:

Microsoft.Practices.ServiceLocation.ActivationException - 
'Activation error occured while trying to get instance of type LogWriter, key ""'
InnerException: Microsoft.Practices.Unity.ResolutionFailedException
which starts:
'Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter", name = "(none)".'
and ends with:
'Resolving Microsoft.Practices.EnterpriseLibrary.Data.Database,DB Connection String'
InnerException: InvalidOperationException
'The type Database cannot be constructed. You must configure the container to supply this value.'

Is there any way to configure the EnterpriseLibrary.Config tool to check Web.config (or App.config, I suppose) for this information?

Element  - Database Settings 
Property - Default Database Instance 
Message  - No Default Database Instance is specified in the Database Settings                                 
Severity - Warning

Element  - DBListener        
Property - Database Instance         
Message  - Reference item is not available.  Select an existing item or add a new item to Database Instances. 
Severity - Warning

Element  - DBTraceListener   
Property - Database Instance         
Message  - Reference item is not available.  Select an existing item or add a new item to Database Instances. 
Severity - Warning
Apr 20, 2012 at 1:01 PM
Edited Apr 20, 2012 at 1:05 PM

If you change 

<enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">

to

<enterpriseLibrary.ConfigurationSource selectedSource="EntLib Configuration Source">

you should be OK.  Then perform all configuration modifications using the EnterpriseLibrary.Config tool editing the web.config.

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