Activation error occured while trying to get instance of type ICacheManager, key “Cache Manager”

前端 未结 1 1504
旧巷少年郎
旧巷少年郎 2021-01-05 05:02

I seem to have hit a wall here and would appreciate some help from anyone who is able to on this one. I am not exactly sure what the error message below means. I am using th

相关标签:
1条回答
  • 2021-01-05 05:34

    The Caching Application Block requires some configuration information to be present in the app/web.config before it can be used (AFAIK, unfortunately it is tough to find documentation stating otherwise). Without that configuration info, the following code will cause that same exception to be thrown as you are seeing:

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

    In order to get a CacheManager, you need to define the CacheManager in your app.config or web.config:

    <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>
    

    Once you add the configuration values, you should be able to get a valid CacheManager from the CacheFactory. The post also has some more information that might help you.

    It is worth noting that if you are using .NET 4.0, for non web apps you can use MemoryCache (in the System.Runtime.Caching namespace) which provides similar functionality but does not require all of this configuration. And for web apps you can of course use the Cache class in System.Web.Caching.

    0 讨论(0)
提交回复
热议问题