Microsoft.Practices.EnterpriseLibrary Caching Error in GetCacheManager

463 Views Asked by At

I am trying to implement enterprise library caching and logging.

I have the latest version (6.0.1304.0) of EnterpriseLibrary.Logging and EnterpriseLibrary.Common

I have the latest version (5.0.505.0) of EnterpriseLibrary.Caching

I am getting the below error when I create cache managerCacheManager cacheManager = (CacheManager)CacheFactory.GetCacheManager();

enter image description here

System.TypeLoadException HResult=0x80131522 Message=Could not load type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer' from assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Source=Microsoft.Practices.EnterpriseLibrary.Caching

Below is my web.config

 <configSections>
  <section name="cachingConfiguration" 
  
type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, 
Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<cachingConfiguration defaultCacheManager="Default Cache Manager">
    <cacheManagers>
        <add name="Default Cache Manager" expirationPollFrequencyInSeconds="60" 
    maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" 
    backingStoreName="inMemory" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager,Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </cacheManagers>
    <backingStores>
        <add name="inMemory" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </backingStores>
</cachingConfiguration>
2

There are 2 best solutions below

0
Antonio Leonardo On

According Microsoft Learn documentation (TypeLoadException Class), this type of exception occurs when the Assembly cannot be found by Common Languange Runtime; like documentation, please, check if the Solution library (DLL) reference point to a different version of Microsoft.Practices.EnterpriseLibrary based on the Web.Config, this maybe the root-cause.

1
Sachin Hayatnagarkar On

I think its because both EnterpriseLibrary.Caching and EnterpriseLibrary.Logging both rely internally on EnterpriseLibrary.Common. As both have different versions used here, there is a version conflict for EnterpriseLibrary.Common. There is usually a backward compatibility but as you are trying to use different versions, it is trying to find version 6.0.1304.0 for EnterpriseLibrary.Common version instead of 5.0.xxx version somewhere internally. I think using 5.0.xxx versions for EnterpriseLibrary.Logging and EnterpriseLibrary.Common should ideally resolve the issue here.