Use of Enterprise Library 5.0 configuration in separate config file

Topics: Exception Handling Application Block, Logging Application Block
Nov 17, 2011 at 2:36 PM

Hello,

I am trying to use enterprise library 5.0 for exceptions logging. When i use it in web.config files i am able to do it successfully. But when i put that configuration in separate file and trying to load that file in my web.config at that it is not working. I am getting this error.

Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl", name = "TestPolicy".Exception occurred while: while resolving.Exception is: InvalidOperationException - The type ExceptionPolicyImpl has multiple constructors of length 2. Unable to disambiguate.-----------------------------------------------At the time of the exception, the container was:
  Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl,TestPolicy

Can anyone help on this.

Here is my web.config file.

<?xml version="1.0"?>

<configuration>

  <configSections>  

   <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> 

</configSections> 

<connectionStrings>   

  <enterpriseLibrary.ConfigurationSource selectedSource="File-based Configuration Source"> 

  <sources>     

<add name="File-based Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"        filePath="C:\POCs\Error Diagnostics\Logging\Logging\EntLib.config" />    

     </sources> 

</enterpriseLibrary.ConfigurationSource> 

<system.web>   

<compilation debug="true" targetFramework="4.0" /> 

</system.web>

 <system.serviceModel>    <behaviors>      <serviceBehaviors>        <behavior>          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->          <serviceMetadata httpGetEnabled="true"/>          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->          <serviceDebug includeExceptionDetailInFaults="false"/>        </behavior>      </serviceBehaviors>    </behaviors>    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />  </system.serviceModel>    <system.webServer>    <modules runAllManagedModulesForAllRequests="true"/>  </system.webServer>  </configuration>

AND Here is my separate Enterprise Library configuration file.  EntLib.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration> 

<configSections>   

<section name="loggingConfiguration" type="System.Configuration.DefaultSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="true" /> 

  <section name="exceptionHandling" type="System.Configuration.DefaultSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="true" />  </configSections>   

<connectionStrings> 

  <add name="log" connectionString="Data Source=.\MSSQLSERVER1;Initial Catalog=Logging;User Id=sa;Password=passw0rd!;"     providerName="System.Data.SqlClient" /> 

</connectionStrings>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="Category">   

<listeners>     

<add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"       listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"       databaseInstanceName="log" writeLogStoredProcName="WriteLog"       addCategoryStoredProcName="AddCategory" formatter="Text Formatter" /> 

  </listeners>

    <formatters>   

<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"       template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"       name="Text Formatter" /> 

  </formatters>   

<categorySources>     

<add switchValue="All" name="Category">     

  <listeners>     

    <add name="Database Trace Listener" />     

  </listeners>     

</add> 

  </categorySources> 

  <specialSources>     

<allEvents switchValue="All" name="All Events" /> 

    <notProcessed switchValue="All" name="Unprocessed Category" /> 

    <errors switchValue="All" name="Logging Errors &amp; Warnings" />

    </specialSources>  </loggingConfiguration>
  <exceptionHandling>    <exceptionPolicies>     

<add name="Policy">        <exceptionTypes>   

      <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"               postHandlingAction="NotifyRethrow">            <exceptionHandlers>              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"               logCategory="Category" eventId="100" severity="Error" title="Enterprise Library Exception Handling"               formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"               priority="0" />            </exceptionHandlers>          </add>        </exceptionTypes>      </add>      <add name="TestPolicy">        <exceptionTypes>          <add name="DivideByZeroException" type="System.DivideByZeroException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"           postHandlingAction="NotifyRethrow">            <exceptionHandlers>              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"               logCategory="Category" eventId="100" severity="Error" title="Enterprise Library Exception Handling"               formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"               priority="0" />            </exceptionHandlers>          </add>        </exceptionTypes>      </add>    </exceptionPolicies>  </exceptionHandling>
</configuration>

Now this is my C# Codes

 public void TestLogging()       

{           

try           

{               

  int j = 0;             

  int i = 100 / j;           

}           

catch (DivideByZeroException ex)         

{               

 bool rethrow = ExceptionPolicy.HandleException(ex, "TestPolicy");      

}       

}

 

Can anyone help on this ?

Nov 18, 2011 at 1:41 AM

In the EntLib.config file the configuration section types are set to System.Configuration.DefaultSection but for logging you should use the type LoggingSettings and for exception handling you should use the type ExceptionHandlingSettings.  I.e:

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />

Also, in your web.config you have an invalid (unclosed) connectionstring tag (but that's probably a typo).

 

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

Nov 18, 2011 at 6:32 PM

Thanks a lot Randy,

You are right as that was the problem.

I guess it was auto generated previously and i copied and pasted in separate file and because of default section it was failing while getting the key of "TestPolicy".

But as you said i changed that two sections and error is resolved. :)))

Aug 14, 2012 at 3:59 PM

Here i m having another problem again.

This is my configuration file. Which is located one project called DAL.

<?xml version="1.0" encoding="utf-8"?><configuration>
  <configSections>   

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />   

<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />  </configSections>
  <connectionStrings>   

<add name="OTESEntities" connectionString="metadata=res://*/OTES_DAL.csdl|res://*/OTES_DAL.ssdl|res://*/OTES_DAL.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=;Initial Catalog=B;Persist Security Info=True;User ID=;Password=;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;"      providerName="System.Data.EntityClient" />  </connectionStrings>  

  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="Category"> 

  <listeners>   

  <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"       listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"       databaseInstanceName="OTESEntities" writeLogStoredProcName="WriteLog"       addCategoryStoredProcName="AddCategory" formatter="Text Formatter" />    </listeners>   

<formatters>     

<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"       template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"       name="Text Formatter" />   

</formatters>   

<categorySources>     

<add switchValue="All" name="Category">   

    <listeners>         

<add name="Database Trace Listener" />       

</listeners>     

</add>   

</categorySources>   

<specialSources>     

<allEvents switchValue="All" name="All Events" />     

<notProcessed switchValue="All" name="Unprocessed Category" />     

<errors switchValue="All" name="Logging Errors &amp; Warnings" />   

</specialSources>  </loggingConfiguration>
  <exceptionHandling>    <exceptionPolicies>      <add name="Policy">        <exceptionTypes>          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"               postHandlingAction="NotifyRethrow">            <exceptionHandlers>              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"               logCategory="Category" eventId="100" severity="Error" title="Enterprise Library Exception Handling"               formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"               priority="0" />           

</exceptionHandlers>         

</add>       

</exceptionTypes>     

</add>         

</exceptionPolicies> 

</exceptionHandling>

</configuration>

The Same Project have file called ExceptionHandler.cs

using System;

using System.Collections.Generic;

using System.Linq;using System.Text;

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Logging;


namespace

{   

public static class ExceptionHandler   

{        public static void Logexception(Exception ex)       

{            ExceptionPolicy.HandleException(ex, "Policy");       

}   

}}

 

Now i am calling this Exception Handler from another ExceptionBAL   class located in BAL Project.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using DAL = DAL dll


namespace

{    public static class ExceptionBAL   

{        public static void Logexception(Exception ex)       

            DAL.ExceptionHandler.Logexception(ex);       

}   

}}

 

 

While calling this ExceptionHandler i m getting this error.

Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

 

I tried 5.0 version as well as this upgraded version also and getting the same problem.

Aug 15, 2012 at 2:46 AM

The configuration file is OK.  The strange thing is that the requested assembly has no PublicKeyToken.  That makes me wonder if the assembly that is not working is somehow referencing another version of the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll (for example a locally built debug version?).  

Check the assembly manifests (using ildasm) to see what the dependencies are.  Also, verify the references in the projects to ensure they are pointing to the expected assembly/version.

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

Aug 15, 2012 at 5:13 PM

I put configuration details in main app.config file of the wpf app and it's working now.

Initially it was in DAL project where as application was looking for it in WPF project.

So it is working now. But there is still problem of referencing dlls

As i reference them from my library folder then it is not working but if i put them directly in the bin folder of the project then only it works.

I don't want to put them in bin folder instead i want to refer them from library folder of the project.'

Any help on this ?

Aug 15, 2012 at 6:17 PM

Assembly resolution is handled by the .NET runtime.  See How the Runtime Locates Assemblies as a starting point.  If you don't wish to deploy your assemblies to the bin directory then you will have to change how the runtime probes for assemblies.  In this case, it can be done using the <codebase> setting. 

For example to load the Logging assembly from the default 32 bit OS installation folder you could use something like this:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Logging"
                  publicKeyToken="31bf3856ad364e35" />
        <codeBase href="file://C:\Program Files\Microsoft Enterprise Library 5.0\Bin\Microsoft.Practices.EnterpriseLibrary.Logging.dll" version="5.0.505.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

You would have to do this for all assemblies you require to run your application that are not in the bin folder.

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

Aug 16, 2012 at 2:28 PM
Edited Aug 16, 2012 at 2:29 PM

I found the exact problem. As Microsoft.Practices.EnterpriseLibrary.Data and Microsoft.Practices.EnterpriseLibrary.Logging.Database

These two libraries are creating the problem. If i reference them from my library folder then it errors out.
but if i put both of them in bin folder then App works fine. I also tried using both one by one. 
So when Data is referenced from the Library folder and Database is in bin folder then i gets error of Data dll is not identifiable
When Database is reference from the Library folder and Data is in bin folder then i gets error of Database Trace listeners are not identifiable.
So is there any thing wrong with this two dlls ?
The solution you suggested is not working out for these two dlls. 
So finally my app works fine having these two dlls in my bin folder. But i still want both these dlls in my Library folder.
Suggest me something for that.


Aug 16, 2012 at 3:10 PM

This sounds like a similar problem to an existing issue with loading assemblies in the GAC.  See http://entlib.codeplex.com/workitem/26903.  The posted solution is to fully qualify the assembly:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <qualifyAssembly partialName="Microsoft.Practices.EnterpriseLibrary.Data" fullName="Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </assemblyBinding>
</runtime>

Hopefully that helps.

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

Aug 16, 2012 at 4:15 PM

Just noticed these in my Project Warnings

Warning 60 The referenced assembly "Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project. Project Name

 

Warning 61 The referenced assembly "Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project. Project Name

 

This may be the main problem that i have to add dll explicitly

 

Aug 16, 2012 at 4:20 PM

You need to compile against the .NET Framework 4 Profile (and not the .NET 4 Client Profile).

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

Aug 17, 2012 at 3:56 PM

Thanks a lot for your help and support, .NET 4 Client Profile was the problem. I changed it to .NET 4 Profile and it is working now. :)