Activation error occured while trying to get instance of type ICacheManager

Topics: Caching Application Block , Data Access Application Block
Sep 20, 2012 at 2:23 PM

Hi all,

Sorry for posting this here, as I am sure this is a simple fix. However of the glut of answers I found, none have so far fixed my problem.

The below error is from a UnitTest. When using Caching.BackingStoreImplementations.NullBackingStore, everything works fine. However, when I switch this to Caching.Database.DataBackingStore, I get the below error. I have also included the app.config, and the code that is causing the error. The Caching database has been created, and the user has access to the database.

Any help at all is appreciated.

Test 'Social.Verify.UnitTest.API.TransactionServiceTest.AddTransactionPass' failed: SetUp : Microsoft.Practices.ServiceLocation.ActivationException : Activation error occured while trying to get instance of type ICacheManager, key "SecurityTokenCacheManager"  ----> Microsoft.Practices.Unity.ResolutionFailedException : Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager", name = "SecurityTokenCacheManager".Exception occurred while: while resolving.Exception is: InvalidOperationException - The type Database does not have an accessible constructor.-----------------------------------------------At the time of the exception, the container was:
  Resolving Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager,SecurityTokenCacheManager (mapped from Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager, SecurityTokenCacheManager)  Resolving parameter "realCache" of constructor Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager(Microsoft.Practices.EnterpriseLibrary.Caching.Cache realCache, Microsoft.Practices.EnterpriseLibrary.Caching.BackgroundScheduler backgroundScheduler, Microsoft.Practices.EnterpriseLibrary.Caching.ExpirationPollTimer pollTimer)    Resolving Microsoft.Practices.EnterpriseLibrary.Caching.Cache,SecurityTokenCacheManager    Resolving parameter "backingStore" of constructor Microsoft.Practices.EnterpriseLibrary.Caching.Cache(Microsoft.Practices.EnterpriseLibrary.Caching.IBackingStore backingStore, Microsoft.Practices.EnterpriseLibrary.Caching.Instrumentation.ICachingInstrumentationProvider instrumentationProvider)      Resolving Microsoft.Practices.EnterpriseLibrary.Caching.Database.DataBackingStore,DataCacheStorage (mapped from Microsoft.Practices.EnterpriseLibrary.Caching.IBackingStore, DataCacheStorage)      Resolving parameter "database" of constructor Microsoft.Practices.EnterpriseLibrary.Caching.Database.DataBackingStore(Microsoft.Practices.EnterpriseLibrary.Data.Database database, System.String databasePartitionName, Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.IStorageEncryptionProvider encryptionProvider)        Resolving Microsoft.Practices.EnterpriseLibrary.Data.Database,CacheDB

 

<configSections>        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />        <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />     </configSections>
    <cachingConfiguration defaultCacheManager="SecurityTokenCacheManager">        <cacheManagers>            <add name="SecurityTokenCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching"                expirationPollFrequencyInSeconds="21600" maximumElementsInCacheBeforeScavenging="1000"                numberToRemoveWhenScavenging="10" backingStoreName="DataCacheStorage" />        </cacheManagers>        <backingStores>            <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching"                name="NullStorage" />            <add name="DataCacheStorage" type="Microsoft.Practices.EnterpriseLibrary.Caching.Database.DataBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching.Database, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"                encryptionProviderName="" databaseInstanceName="CacheDB"                partitionName="token" />        </backingStores>    </cachingConfiguration>    
    <dataConfiguration defaultDatabase="CacheDB" />    <connectionStrings>        <add name="CacheDB" connectionString=" Server=.\SQLEXPRESS;initial catalog=Caching;Integrated Security=SSPI" />    </connectionStrings>

 

The offending code is here (CacheFactory.GetCacheManager).

 

using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using StructureMap;
using Microsoft.Practices.EnterpriseLibrary.Caching;
namespace Core{    public class IocRegistry    {        public static void SetupIoc()        {            ObjectFactory.Initialize(x =>                {                       x.For<ICacheManager>().Use(CacheFactory.GetCacheManager(Constants.TOKENCACHEMANAGER));
                    Debug.WriteLine(ObjectFactory.WhatDoIHave());
                }            );        }    }}

Sep 21, 2012 at 12:12 AM

Add the providerName to the connectionString entry:

  <connectionStrings>
    <add name="CacheDB" connectionString=" Server=.\SQLEXPRESS;initial catalog=Caching;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
  </connectionStrings>

If the providerName is missing (or incorrect) then the connection information will not be added to the container and then an ActivationException will usually occur.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Sep 21, 2012 at 8:44 AM

Thank you very much Randy, this solved our problem. We feel a bit sheepish over that one :)

Mar 7, 2013 at 8:09 PM
I have tried, referencing to the correct DLLs, checking my trace listener configuration so many times, choosing framework and nothing worked. finally I have seen that providerName="System.Data.SqlClient" was missing. Thank you it resolved my problem.