企业库异常:尝试获取ICacheManager类型的实例时发生激活错误,密钥?

时间:2014-06-02 04:30:13

标签: c# .net caching enterprise-library

使用.net 3.5和Enterprise library 5.0。

从我的研究中,我发现了类似的问题: Activation error occured while trying to get instance of type ICacheManager, key "Cache Manager" ***此解决方案无法解决我的问题。

我似乎无法弄明白,我的配置应该正确设置,但一直得到异常?有人有类似的问题吗?

我建议在调用缓存管理器时添加cacheManager和引用:

using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
.....
....
ICacheManager cm = CacheFactory.GetCacheManager("TrackingCacheManager");

App.config:

<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <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="TrackingCacheManager">
        <cacheManagers>
            <add name="TrackingCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                expirationPollFrequencyInSeconds="120" maximumElementsInCacheBeforeScavenging="1000"
                numberToRemoveWhenScavenging="10" 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>
    <dataConfiguration defaultDatabase="ConnectionString" />
    <connectionStrings>
        <add name="ConnectionString" connectionString="server=AFS7BCBRNGQ5O0\DEVELOPMENT;database=EITC_RTS;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

我的推荐信:

enter image description here

2 个答案:

答案 0 :(得分:4)

我的猜测是您已在域项目的App.config中设置此配置。但是您的主项目有自己的配置文件,必须将此配置复制到其中。

因此,例如,如果您的主项目是一个webapp,那么它将有一个web.config。您在运行时未使用已添加到Domain项目的App.config的缓存配置。正在使用的配置来自主项目的配置,在本例中为web.config。

将您的缓存配置从域App.Config复制到主配置文件,它将起作用。

答案 1 :(得分:0)

我犯了愚蠢的错误,但在阅读上述内容后,了解问题。这是我的vb.net代码,它给出了上述错误。

Imports System.Collections
'Imports System.Configuration

Public Class DatabaseLogic
    Public ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("db").ToString()

    Public Function ServerDataMagic(StoredProcedure As String, PDMdata As Hashtable) As DataSet
        Dim db As Database = DatabaseFactory.CreateDatabase(ConnectionString )  'Here  I am getting error.
        Using cmd As DbCommand = db.GetStoredProcCommand(StoredProcedure)
            Try
                db.DiscoverParameters(cmd)
            Catch discover_ex As Exception

            End Try

并在web.config条目中

<add name="db" connectionString="Database=Dbname;Server=SERVER;uid=sa;pwd=sa@1234" providerName="System.Data.SqlClient" />

读完之后问题就是CreateDatabase方法需要配置条目密钥作为字符串,我通过配置条目访问获得了确切的连接字符串。这是我更新的代码。

  Dim db As Database = DatabaseFactory.CreateDatabase("db")  'Here I changed the config entry key

我发帖给别人帮忙。