此特定类型项目和Ent Lib 5.0存在以下(但非常烦人)问题。
我之所以说它与单元测试项目相关的事实是因为相同的代码/配置可以从控制台应用程序中正常工作。
以下是代码:
ICacheManager cm = CacheFactory.GetCacheManager("TestCacheManager");
cm.Add("max", 1);
这是配置:
<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>
<dataConfiguration defaultDatabase="Caching" />
<connectionStrings>
<add name="Caching" connectionString="Data Source=.;Initial Catalog=Caching;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<cachingConfiguration defaultCacheManager="TestCacheManager">
<cacheManagers>
<add name="TestCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="TestStore" />
</cacheManagers>
<backingStores>
<add name="TestStore" type="Microsoft.Practices.EnterpriseLibrary.Caching.Database.DataBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
encryptionProviderName="" databaseInstanceName="Caching"
partitionName="Name" />
<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>
然后最后我才进入单元测试项目的例外:
Activation error occured while trying to get instance of type ICacheManager, key "TestCacheManager"