0

I am using CacheManager.Net, I am trying to add a CacheItem to a ICacheManager object, but I get the following exception "Object reference not set to an instance of an object.".

Following is some code snippet:

public class ReaderController{

private ICacheManager<object> tagReadEventsCache;

public ReaderController()
{
tagReadEventsCache = CacheFactory.Build("tagReadEventsCache", settings => settings
.WithUpdateMode(CacheUpdateMode.Up)
.WithSystemRuntimeCacheHandle("Zones")
.WithExpiration(ExpirationMode.Sliding, TimeSpan.FromMilliseconds(1000)));

tagReadEventsCache.OnAdd += TagReadEventsCache_OnAdd;
}

private void TagReadEventsCache_OnAdd(object sender,                                              CacheManager.Core.Internal.CacheActionEventArgs e)
{
generalLog.Debug(e.Key + " - " + e.Region + " - " + e.GetType().ToString());
}

public void AddTagReadEventCache(List<TagRead> tagReadEvent)
{
  foreach (TagRead tag in tagReadEvent)
  {
   try
   {
     var item = new CacheItem<object>(tag.Epc, tag, ExpirationMode.Sliding,     TimeSpan.FromMilliseconds(10000));
                    //tagReadEventsCache.Put(item);
                    tagReadEventsCache.Add(item);
   }
   catch (Exception ex)
   {
    generalLog.Error("Excepcion tag " + tag.ReaderHostName, ex);
   }
  }
}
....

}

So when the AddTagReadEventCache method is called it generates the exception when it tries to add an Cacheitem, also I did try the Put method and happens the same. I know it should be some small thing I am missing somewhere, But I am stock, I really appreciate someone help.

Thanks

MarioV
  • 108
  • 11
  • Hi MarioV, are you sure the null reference exception doesn't occur elsewhere? What is the exact error message/stack trace? Also, is this an MVC controller or what exactly is that? If it is MVC or WebAPI, creating the cache on each request in the ctor is not a good idea – MichaC Feb 16 '17 at 20:11
  • @MichaC , I am working on Windows service, creating an RFID Middleware, the Cache it is only instantiated when the controller instance is created, the exception was fired when I was trying to Add an CacheItem into the Cache Object, but I notice that the instance of the Cache was null, that was why it was throwing an exception when trying to Add the CacheItem. Problem solved. – MarioV Feb 17 '17 at 20:32

0 Answers0