Activation error - ICacheManager

Topics: Caching Application Block
Aug 27, 2010 at 11:53 PM
Edited Aug 29, 2010 at 3:48 AM

Hi, I am using Ent Lib 5.0 in a WCF Service Library (.NET 3.5). I had GACed the necessary ent lib dlls.

When using the DAAB, I ran into 'Activation error occurred while trying to get an instance of type Database, key'. This seems to be an issue when using GACed entlib dlls and was resolved by referencing this issue log - http://entlib.codeplex.com/workitem/26903?ProjectName=entlib (adding the workaround typeRegistrationProvidersConfiguration to the config file)

Just to be on safe side for Caching, I added the below to the typeRegistrationProvidersConfiguration node

    <remove name="Caching" />
    <add name="Caching" providerType="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

Any ideas/leads is much appreciated.

Exception: {"Activation error occured while trying to get instance of type ICacheManager, key \"xyz\""}

Inner Exception: {"The current type, Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager, is an interface and cannot be constructed. Are you missing a type mapping?"}

Message: "Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager\", name = \"xyz\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The current type, Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager, is an interface and cannot be constructed. Are you missing a type mapping?\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n  Resolving Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager,xyz\r\n"

Stack Trace: "   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)\r\n   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides)\r\n   at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)\r\n   at Microsoft.Practices.Unity.UnityServiceLocator.DoGetInstance(Type serviceType, String key)\r\n   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)"

Here's my config

<?xml version="1.0" encoding="utf-8" ?>
<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" />
    <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="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </configSections>
  <cachingConfiguration defaultCacheManager="xyz">
    <cacheManagers>
      <add name="xyz" 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="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="test" />
  <connectionStrings>
    <add name="test" connectionString="Data Source=testdb;Initial Catalog=testdb123;User Id=guest;Password=Passw0rd"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <typeRegistrationProvidersConfiguration>
    <remove name="Data Access" />
    <add name="Data Access" providerType="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSyntheticConfigSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <remove name="Validation" />
    <add name="Validation" providerType="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationTypeRegistrationProvider, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <remove name="Caching" />
    <add name="Caching" providerType="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </typeRegistrationProvidersConfiguration>
... followed by wcf config info...

Here's my code:

    public class Service1 : IService1
    {
        private Database mytestdb;
        private ICacheManager TESTCacheManager;
        public Service1(Database DB)
        {
            mytestdb = DB;
        }
        public Service1()
        {
            TESTCacheManager = CacheFactory.GetCacheManager("xyz");
        }
        private DataSet GetDataFromDB()
        {
            Database mytestdb = DatabaseFactory.CreateDatabase("test");
            DbCommand mytestdbCommand = mytestdb.GetStoredProcCommand("myproc");
            DataSet ds = new DataSet();
            mytestdb.AddInParameter(mytestdbCommand, "@ClientName", DbType.String, "abcxyz");
            mytestdb.LoadDataSet(mytestdbCommand, ds, "SubmitterList");
            return ds;
        }
        public string GetData(int value)
        {
             DataSet ds1 = (DataSet)TESTCacheManager.GetData("SUBMITTERLIST_abcxyz");
            if (ds1 == null)
            {
                ds1 = GetDataFromDB();
                if (ds1 != null)
                {
                    TESTCacheManager.Add("SUBMITTERLIST_abcxyz", ds1);
                }
            }
            return string.Format("Number of abcxyz Submitters: {0}", ds1.Tables[0].Rows.Count.ToString());
        }
 }
Aug 29, 2010 at 3:51 AM

Hello,

It seems the info that I added Caching in typeRegistrationProvidersConfiguration is not needed.

    <remove name="Caching" />
    <add name="Caching" providerType="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

Removing this piece from my config solved the issue - though am not sure how it caused the issue first hand (technically, I am providing the fully qualified name for the caching dll)

Thanks!

Aug 31, 2010 at 6:40 AM

Unfortunately I really don't know if adding typeRegistrationProvidersConfiguration for other app block is still necessary given that the issue is only specific for VAB and DAAB. Given this, we'll check with the entlib team their thoughts regarding this.

Anyway, I'm glad you were able to find the cause of your problem and hoping it could also help anyone in the community. Thanks.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 



        
    
Sep 12, 2011 at 6:38 PM

I am getting this error also.  The situation I have is that if the client application has configured the caching block, I want to cache some values in a dependent assembly.  If the client has not configured the caching block, I want the dependent assembly to ignore the cache and retrieve the value each time.  

Is there some sort of way to see if the client has caching configured before the call to GetInstance<ICacheManager>()?

Sep 13, 2011 at 2:59 AM
craigk0609 wrote:

Is there some sort of way to see if the client has caching configured before the call to GetInstance<ICacheManager>()?

I believe you can check the application's configuration file by calling the ConfigurationManager class' GetSection method and passing in the name of the section.