CacheFactory.GetCacheManager() generates NullReferenceException

Topics: Caching Application Block
Jan 15, 2008 at 7:27 PM
I'm trying to use the CAB in a class library project for the data access layer of a project.

Following all the documentation that I've found, I'm trying to get a CacheManager instance by using this line:

_cache = CacheFactory.GetCacheManager("Default Cache Manager");

Every time the debugger hits it, it generates a System.NullReferenceexception.
Here's the stack trace:

at Microsoft.Practices.EnterpriseLibrary.Caching.CachingConfigurationView.GetCacheManagerData(String cacheManagerName)
at Microsoft.Practices.EnterpriseLibrary.Caching.CacheManagerCustomFactory.GetConfiguration(String id, IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Caching.CacheManagerCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUpTTypeToBuild(IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUpT(IReadWriteLocator locator, String id, IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.LocatorNameTypeFactoryBase`1.Create(String name)
at Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory.GetCacheManager(String cacheManagerName)
at AtoZ.Content.DataAccess.ContentCache..cctor() in C:\source\AtoZContentManagerSample\AtoZ.Content.DataAccess\ContentCache.cs:line 32

Here's my app.config contents:

<?xml version="1.0" encoding="utf-8"?>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add name="something"
providerName="System.Data.SqlClient" />
<cachingConfiguration defaultCacheManager="Default Cache Manager">
<add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
name="Default Cache Manager" />
<add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Null Storage" />

I'm using VS2008 and EntLib 3.1.
Target framework is .NET 3.5.

I've included references to the binaries for the EntLib.Caching and EntLib.Common DLLs.

Any ideas how to get it working?

Jan 30, 2008 at 12:46 AM
I am having the EXACT same error and I am also using VS2008 with EntLib 3.1 and .NET FW 3.5
Only difference is my call uses an empty ctor

_cache = CacheFactory.GetCacheManager();

I hanvn't debugged into the entlib code but if it comes to that and I resolve the issue I will post the resolution.
Jan 31, 2008 at 7:49 AM
See this:
may be it will solve your problem

Kiran Patil
Feb 10, 2009 at 10:28 PM
I see this error sporadically at customer site. Deleting the IsolatedStorage data seems to fix the problem. Appears to be a corruption of the cached data. If anyone has any thoughts on the cause or a way to handle the exception in the call to CacheFactory.GetCacheManager(...), please let me know. Thanks.
Feb 11, 2009 at 6:23 AM

You can just do a regular try.. catch statement when calling CacheFactory.GetCacheManager, just handle the exception the way you want to.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Feb 11, 2009 at 2:06 PM
I guess it's handling the exception "the way you want to" I am struggling with. Ideally, I need a reference to that CacheManager. I'm not seeing any way to get that or even to clear it so I can start with an empty Cache again. My only thought at the momment is to programatically reference the Cache physical directory in Isolated Storage and delete the contents and try again.
Feb 12, 2009 at 3:00 AM
See the suggestion by Fernando here -

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
Feb 12, 2009 at 2:08 PM
I had come to a similar solution as Fernando's suggestion:


    _cacheManager =
catch (Exception ex)
//cache is corrupt
    bool rethrow = ExceptionPolicy.HandleException(ex, "Handle and Resume Policy"); //log the issue
    IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("ParameterCache"); //get the isolated storage area
    backingStore.Flush(); //clear it
    _cacheManager = CacheFactory.GetCacheManager(LocalStorageNames.Parameters); //start with an empty CacheManager


Dec 1, 2009 at 6:12 AM

Thanks for your solution bmoran. It was exactly what I was looking for. I simply replaced "ParameterCache" with my backingstore's partionName and now, whenever the cache becomes corrupt, I can programmatically fix it. Before I had to resort to manually deleteting the isolated storage directory. One less headache to worry about - thanks!