使用Microsoft.Practices.EnterpriseLibrary缓存时GetCacheManager报错问题
问题描述
我正尝试实现EnterpriseLibrary缓存与日志功能,已安装最新版本(6.0.1304.0)的EnterpriseLibrary.Logging和EnterpriseLibrary.Common,以及最新版本(5.0.505.0)的EnterpriseLibrary.Caching。执行代码CacheManager cacheManager = (CacheManager)CacheFactory.GetCacheManager();创建缓存管理器时,抛出以下错误:
错误信息
System.TypeLoadException HResult=0x80131522 Message=无法从程序集'Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'中加载类型'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer'。 Source=Microsoft.Practices.EnterpriseLibrary.Caching
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>
解决方案
这个错误的核心是版本不兼容:
- EnterpriseLibrary 5.0的Caching组件依赖
Microsoft.Practices.EnterpriseLibrary.Common中的EnterpriseLibraryContainer类,但该类在6.0版本的Common组件中已被移除重构。 - 同时引用5.0的Caching和6.0的Common,导致运行时加载不到Caching所需的类型。
解决步骤:
- 统一组件版本:卸载5.0版本的
EnterpriseLibrary.Caching,安装与Logging、Common同版本(6.0.1304.0)的Caching组件。 - 更新配置文件:将web.config中所有Caching相关配置的版本号从
5.0.505.0替换为6.0.0.0(注:6.0版本程序集版本号为6.0.0.0,文件版本为6.0.1304.0)。 - 调整初始化代码:6.0版本不再使用
CacheFactory.GetCacheManager(),改用EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>()。
调整后的web.config片段示例
<configSections> <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=6.0.0.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=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </cacheManagers> <backingStores> <add name="inMemory" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </backingStores> </cachingConfiguration>
调整后的初始化代码示例
using Microsoft.Practices.EnterpriseLibrary.Caching; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; ICacheManager cacheManager = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>();
内容的提问来源于stack exchange,提问作者Jey
相关产品推荐
相关产品推荐

