InvalidCastException: Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings. Conflicts with Ent Lib Ver 5 & 6

Topics: Enterprise Library Core, Logging Application Block
Sep 3, 2014 at 11:14 AM

I am getting following invalid cast exception:

System.InvalidCastException: [A]Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings cannot be cast to [B]Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings.

Type A originates from 'Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\windows\assembly\GAC_MSIL\Microsoft.Practices.EnterpriseLibrary.Logging\5.0.414.0__31bf3856ad364e35\Microsoft.Practices.EnterpriseLibrary.Logging.dll'.

Type B originates from 'Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Practices.EnterpriseLibrary.Logging\v4.0_6.0.0.0__31bf3856ad364e35\Microsoft.Practices.EnterpriseLibrary.Logging.dll'.

Config Sections configured in Config files are as below:


<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="auditingClientHelperLoggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />


Kindly, let me know the resolution for the same.

Sachin R. Chavan.
Sep 4, 2014 at 7:23 AM
It looks like you are trying to run both Enterprise Library 5 and Enterprise Library 6 in the same app domain. The easiest solution would be to only use one version of Enterprise Library. There may be some other options (using the GAC or sub-folders) depending on the specific scenario.

Randy Levy
Enterprise Library support engineer
Support How-to
Sep 5, 2014 at 5:58 AM
Will providing following assembly redierct configuration help? Or there will still be conflicts?

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

        <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Logging"
                          culture="neutral" />
        <bindingRedirect oldVersion="5.0.414.0"  newVersion=""/>

        <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Common"
                          culture="neutral" />
        <bindingRedirect oldVersion="5.0.414.0" newVersion=""/>


If not why?
Is there anywork around to make this work without updating the code using Ent. lib. ver. 5?

Code using EntLib. 5 is a web app and Code using EntLib. 6 is a separete dll (assembly).

Will loading the assembly using Ent. Lib. 6 in sepearte app domain help?

Kindly, let me know.
Sep 7, 2014 at 7:18 AM
Enterprise Library Logging requires the configuration section to be called loggingConfiguration. That's why you are seeing the error -- Enterprise Library 6 is trying to retrieve the "loggingConfiguration" section but the type of loggingConfiguration is Version 5.0.414.0 so it causes an error.

The easiest work around is to separate out the configuration into separate configuration files and then have one, or both, of the versions explicitly load a configuration file. It might make more sense to do this for EntLib 6 since it already requires some bootstrapping to load the configuration so it would be a small change to the code.

So you can move the EntLib6 config into EntLib6.config and deploy that config file to the output folder. Then change the bootstrapping to load the EntLib6.config file explicitly:
var config = new FileConfigurationSource("EntLib6.config");
var factory = new Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterFactory(config);

With that change, you should be able to run both versions side by side (assuming the runtime can locate the assemblies).

Randy Levy
Enterprise Library support engineer
Support How-to
Marked as answer by SachinRChavan on 5/26/2015 at 6:20 AM
Sep 10, 2014 at 6:27 AM
Any full sample application in github or codeplex using Ent.Lib 5.0 and Ent.Lib 6.0 with several configuration files ?

Anyways, Any full sample application in github or codeplex using Ent.Lib 6.0 and GOOD PATTERNS AND PRACTICES?