CAB Null Reference Issue

Topics: Caching Application Block
Sep 26, 2007 at 3:23 PM
I am attempting to use the Caching Application Block for Enterprise Library 2007. It works perfectly fine when running code, however the block seems to be destroying the ability to use the visual designer on any of my forms.

I get the following error message when I open the designer on any form:

Object reference not set to an instance of an object:

at Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerDataRetriever.MapName(String name, IConfigurationSource configurationSource)
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, IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.LocatorNameTypeFactoryBase`1.CreateDefault()
at Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory.GetCacheManager()

If I comment out the line of _repositoryCache = CacheFactory.GetCacheManager(); (this is the line the error is complaining about) the visual designer seems to work fine. If I wrap that line in a try catch block it restores the visual designer for most of the forms. I am using visual inheritance on these forms so I have a userControlContext that is using the cache manager and it is the inherited forms that are breaking. I would like to retain the ability to use the visual designers for ease of use. Any ideas would be greatly appreciated. I would really hate to have to re-create the entire block from scratch to get it to work with the visual designers in this instance.
Sep 28, 2007 at 3:10 PM

Do you need the CAB's functionality at design time? I suggest to avoid using it at design time based on your control's/form's DesignMode property.
Please see this related post

Hope this helps,
Sep 28, 2007 at 7:31 PM
That explains the issue perfectly. Thanks so much. I wound up moving the construction of the cache further up the chain, but your explanation and solution is far better.