Using the Caching Application Block with Unity 2.0

May 11, 2010 at 7:11 PM

I have successfully converted my unity 1.2 config file to unity 2.0 (quite some work, actually). Then I tried to use the Caching Application Block from Enterprise Library 5 together with Unity without success.

My difficulties are with the new sectionExtensions elements. I tried the following:

    <sectionExtension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common"/>
    <sectionExtension type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.Unity.CachingBlockExtension, Microsoft.Practices.EnterpriseLibrary.Caching"/>

The exception I get is the following:

"The extension type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common does not derive from SectionExtension"

Which I understand, so is there a class that inherits from sectionExtension and supports the Caching Block? How would I configure it?





May 12, 2010 at 1:14 AM

The <sectionExtension> element is a means for you to extend the unity configuration schema.  The InterceptionConfigurationExtension is an example of a section extension.

You should still put the EnterpriseLibraryCoreExtension in the <extensions> element.  The CachingBlockExtension as well as the other block-specific extensions has been deprecated and their functionality have been moved to the  EnterpriseLibraryCoreExtension.

Keep in mind that <extensions> goes under a <container> element while <sectionExtension> goes directly under the <unity> element.  Please refer to the Unity Configuration Schema topic in the documentation for more info.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.

May 12, 2010 at 9:53 AM


That worked, I can now resolve the Cache via ICacheManager mgr = CacheFactory.GetCacheManager(); but I would like to resove it via Unity with container.Resole<Cache>() and this does not work, I receive an exception like:

Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Caching.Cache", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, Microsoft.Practices.EnterpriseLibrary.Caching.IBackingStore, is an interface and cannot be constructed. Are you missing a type mapping?
At the time of the exception, the container was:

  Resolving Microsoft.Practices.EnterpriseLibrary.Caching.Cache,(none)
  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.IBackingStore,(none)

I am missing somethins in my Unity.config?

I have added:

    <container name="AidaContainer">

      <extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />

Many thanks


May 12, 2010 at 10:09 AM
Edited May 12, 2010 at 10:09 AM

Just some additional info:

CacheManager cc = container.Resolve<CacheManager>();

Does not work either.





May 12, 2010 at 10:17 AM



CacheManager cc = container.Resolve<CacheManager>("Cache Manager");


Many thanks



May 12, 2010 at 10:38 AM
Edited May 12, 2010 at 10:43 AM

Actually, you should be specifying ICacheManager instead of CacheManager.  Specifying the actual implementation doesn't take advantage of using interfaces. If you're resolving for the default Cache Manager, container.Resolve<ICacheManager>() should work, you need not specify a registration key. 

I have forgot to mention that you could also use


to resolve an ICacheManager.  You need not add the EnterpriseLibraryCoreExtension when using this approach.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.

May 12, 2010 at 8:12 PM

Cache and CacheManager are not the public entry points to the caching block, they're internal implementation details. The public entry point is, as Sarah said, ICacheManager.