我似乎在这里碰到了墙,希望能从任何能够做到这一点的人那里得到一些帮助。我不确定以下错误消息的含义。我正在使用企业模式服务的缓存块,但是我一直遇到以下问题。我下载了最新版本,并尝试逐步解决该问题,但是我似乎无法找出确切的问题,因此需要帮助。提前致谢

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

最佳答案

缓存应用程序块需要在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命名空间中),该功能提供类似的功能,但不需要所有此配置。对于Web应用程序,您当然可以使用System.Web.Caching中的Cache类。

关于c# - 尝试获取ICacheManager类型的实例,键 "Cache Manager"时发生激活错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6234076/

10-10 21:55