This project is read-only.
1

Closed

The CacheFactory.FromConfiguration throws an exception with default app.config settings

description

Hey,

I'm trying to include CacheManager in the Business Logic layer of my project.
I created a BaseBL with the following implementation:
public abstract class BaseBL
    {
        private ICacheManager<object> _defaultCache;

        public ICacheManager<object> Cache
        {
            get
            {
                if (_defaultCache == null)
                {
                    _defaultCache = CacheFactory.FromConfiguration<object>("Default");
                }
                return _defaultCache;
            }
        }
    }
Then I used it like so:
var cacheItem = Cache.GetCacheItem(cacheKey);
But it throws ArgumentNullException with the message "Value cannot be null.
Parameter name: section".
Here's the stack trace:
   at CacheManager.Core.Configuration.ConfigurationLoader.LoadFromSection(CacheManagerSection section, String configName)
   at CacheManager.Core.Configuration.ConfigurationLoader.LoadConfiguration(String configName)
   at CacheManager.Core.Cache.CacheReflectionHelper.FromConfiguration[TCacheValue](String name)
   at CacheManager.Core.CacheFactory.FromConfiguration[T](String name)
   at CacheManager.Core.CacheFactory.FromConfiguration(String name)
   at TaxU.Server.BL.Managers.BaseBL.GetCacheHandle(String name) in d:\Documents\SkyDrive (2)\Projects\ייעוץ מס\4-פיתוח\TaxU.Server\TaxU.Server.BL\Managers\BaseBL.cs:line 18
   at TaxU.Server.BL.Managers.ClientsBL.GetAllClients() in d:\Documents\SkyDrive (2)\Projects\ייעוץ מס\4-פיתוח\TaxU.Server\TaxU.Server.BL\Managers\ClientsBL.cs:line 42
By the way, I didn't change the default app.config, here it is anyway:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="cacheManager" type="CacheManager.Core.Configuration.CacheManagerSection, CacheManager.Core" />
  </configSections>
  <cacheManager xmlns="http://tempuri.org/CacheManagerCfg.xsd">
    <!-- Add your custom cache configuration here -->
    <managers>
      <!-- you can define as many named caches as you want and initialize them with CacheFactory.LoadConfiguration(name) -->
      <!-- example cache -->
      <cache name="DefaultCache" updateMode="Up" enableStatistics="true" enablePerformanceCounters="true">
        <handle ref="genericDictionary" name="myHandle" expirationMode="Absolute" timeout="20m" />
      </cache>
    </managers>
  
    <cacheHandles>
      <!-- example handle -->
      <handleDef id="genericDictionary" type="CacheManager.Core.Cache.DictionaryCacheHandle`1, CacheManager.Core" />
      <!-- all available cache handles must be registered here before you can use it from managers configuration -->
    <handleDef id="sysCache" type="CacheManager.SystemRuntimeCaching.MemoryCacheHandle`1, CacheManager.SystemRuntimeCaching" /></cacheHandles> 
  </cacheManager>
</configuration>
Closed Feb 19, 2014 at 12:37 PM by MichaConrad

comments

MichaConrad wrote Feb 19, 2014 at 10:52 AM

Hi Kedmyster,
Thanks for posting the first issue ;)

You code is actually working for me. The error indicates that the cacheManager section cannot be found.
I already changed the code to not throw an ArgumentNullException in this case and instead it will throw
throw new InvalidOperationException("No cacheManager section defined.");
I think in your case I guess you have a console application and another assembly which contains your implementation and the app.config right?
If so, you'll have to add the nuget packages and the app.config/cache configuration to that console application. Because this is how the Microsoft configuration framework works, it always looks into the config file of the running application and actually doesn't care about assemblies.

On a side node, you have defined your cache name as "DefaultCache" within the app config, but you are trying to load "Default" in code. Those names need to match.

Kedmyster wrote Feb 19, 2014 at 12:07 PM

Hey,

I'm an early-adapter in my nature ;)
I know about the mismatch, I edited my issue (more than once) and probably made it by mistake.
As for your tip, that actually fixed my problem, thanks a lot!

MichaConrad wrote Feb 19, 2014 at 12:20 PM

Awesome!
Feel free to send me feedback of what you find good or bad or post feature request, too.
I'm still working on the details...

Thanks
M