尝试获取ICacheManager类型的实例时出现激活错误,键“缓存管理器”

时间:2011-06-04 01:28:59

标签: c# .net caching unity-container enterprise-library

我似乎在这里遇到了障碍,并希望得到任何能够参与此活动的人的帮助。我不确定下面的错误消息是什么意思。我正在使用企业模式服务的缓存块,但我继续遇到下面的问题。我下载了最新版本,并尝试逐步解决这个问题,但我似乎无法解决确切的问题,我需要帮助。提前致谢

Test method WorldBank.Service.Business.UnitTest.TopicsManagerTest.Call_Children_out_of_schoolTest threw exception: 
Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ICacheManager, key "Cache Manager" ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager", name = "Cache Manager".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:

  Resolving Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager,WuCache
 ---> System.InvalidOperationException: The current type, Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager, is an interface and cannot be constructed. Are you missing a type mapping?


Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs: line 209
BuildUp_Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager(IBuilderContext )
Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlan.cs: line 37
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs: line 43
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs: line 110
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs: line 511
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs: line 515
Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs: line 173
Microsoft.Practices.Unity.UnityServiceLocator.DoGetInstance(Type serviceType, String key) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityServiceLocator.cs: line 64
Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs: line 49
Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs: line 53
Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs: line 103
Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory.InnerGetCacheManager(String cacheManagerName) in e:\Builds\EntLib\Latest\Source\Blocks\Caching\Src\Caching\CacheFactory.cs: line 66
Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory.GetCacheManager(String cacheManagerName) in e:\Builds\EntLib\Latest\Source\Blocks\Caching\Src\Caching\CacheFactory.cs: line 53
WorldBank.Service.Business.ResponseManager.Get_w_Utility() in C:\SOOfficialPiece\WorldBank.Service.Business\ResponseManager.cs: line 42
WorldBank.Service.Business.ResponseManager..ctor() in C:\SOOfficialPiece\WorldBank.Service.Business\ResponseManager.cs: line 34
WorldBank.Service.Business.ResponseManager.getResponseObjectJSON(Int32 perpage, Object results, Boolean doDeepRetrieval) in C:\SOOfficialPiece\WorldBank.Service.Business\ResponseManager.cs: line 68
System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
WorldBankBusiness.Topics.TopicsManager.Call_Children_out_of_school(PersonType persontype, EnrollmentType enrollmenttype, String countrycode) in C:\SOOfficialPiece\WorldBank.Service.Business\TopicsManager.cs: line 39
WorldBank.Service.Business.UnitTest.TopicsManagerTest.Call_Children_out_of_schoolTest() in C:\SOOfficialPiece\WorldBank.Service.Business.UnitTest\TopicsManagerTest.cs: line 88

1 个答案:

答案 0 :(得分:18)

缓存应用程序块需要一些配置信息才能在app / web.config中使用(AFAIK,遗憾的是很难找到文档说明其他情况)。如果没有该配置信息,以下代码将导致抛出相同的异常:

var cm = CacheFactory.GetCacheManager("MyCacheManager");

要获得CacheManager,您需要在app.config或web.config中定义CacheManager

<configuration>
    <configSections>
        <section name="cachingConfiguration" 
                 type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <cachingConfiguration defaultCacheManager="MyCacheManager">
        <cacheManagers>
           <add name="MyCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
                expirationPollFrequencyInSeconds="60" 
                maximumElementsInCacheBeforeScavenging="50000" 
                numberToRemoveWhenScavenging="1000"  
                backingStoreName="NullBackingStore" />
        </cacheManagers>
        <backingStores>
            <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
                 name="NullBackingStore" />
        </backingStores>
    </cachingConfiguration>
</configuration>

添加配置值后,您应该能够从CacheManager获得有效的CacheFactorypost还有一些可能对您有帮助的信息。

值得注意的是,如果您使用的是.NET 4.0,对于非Web应用程序,您可以使用MemoryCache(在System.Runtime.Caching命名空间中),它提供类似的功能,但不需要所有这些配置。对于网络应用,您当然可以使用System.Web.Caching中的Cache课程。