Base solution for your next web application
Open Closed

Null Reference exception when using ICacheManager.... #472


User avatar
0
theedge created

Hi,

I am using ABP 0.7.3.

I have a cache service that uses IoC to inject a ICacheManager. This cache service is created from the constructor of my MVC controller also using IoC.

I call the cache service from a method off the above controller and get a NullReference exception. I read the doco about not calling GetCache() in a constructor, but as this is ultimately being called from a regular controller method it is not that.

I include a screenshot of my code (attached) showing that all the necessary objects are instantiated.

Any pointers greatly appreciated.


7 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Your code seems corrent. I think _CacheManager and brandCacheBySeo are not null. Can you check it? Also, please share your stack trace to understand which method throws this exception.

  • User Avatar
    0
    theedge created

    Hi Halil,

    Yep both of those references are not NULL. Here is the call stack as requested:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Abp.Runtime.Caching.CacheExtensions.GetOrDefault(ICache cache, TKey key) +53
       OzCruisingPlatform.Application.Services.Cache.OzCpCacheService.GetReferenceBrandBySeoFriendlyName(OzCpGetReferenceBrandBySeoFriendlyNameInput aParams) in D:\Data\TFS\Ozcruising\VS 2015\OzCruisingPlatform\OzCruisingPlatform.Application\Services\Cache\OzCpCacheService.cs:86
       Castle.Proxies.OzCpCacheServiceProxy.GetReferenceBrandBySeoFriendlyName_callback(OzCpGetReferenceBrandBySeoFriendlyNameInput aParams) +56
       Castle.Proxies.Invocations.IOzCpCacheService_GetReferenceBrandBySeoFriendlyName.InvokeMethodOnTarget() +179
       Castle.DynamicProxy.AbstractInvocation.Proceed() +110
       Castle.DynamicProxy.AbstractInvocation.Proceed() +447
       Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) +62
       Castle.DynamicProxy.AbstractInvocation.Proceed() +447
       Castle.DynamicProxy.AbstractInvocation.Proceed() +447
       Castle.DynamicProxy.AbstractInvocation.Proceed() +447
       Castle.Proxies.OzCpCacheServiceProxy.GetReferenceBrandBySeoFriendlyName(OzCpGetReferenceBrandBySeoFriendlyNameInput aParams) +255
       CruisesDesktop.Web.Controllers.CruisesController.ByBrand(String aBrandNameSeoFragment) in D:\Data\TFS\Ozcruising\VS 2015\CruisesDesktop\Cruises.Web\Controllers\CruisesController.cs:37
       lambda_method(Closure , ControllerBase , Object[] ) +141
       System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +228
       System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +34
       System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +38
    
  • User Avatar
    0
    theedge created

    Bump!!!

  • User Avatar
    0
    hikalkan created
    Support Team

    Is your key null? aParams.BrandSeoFriendly? Because the code throws exception is very simple:

    public static TValue GetOrDefault<TKey, TValue>(this ICache cache, TKey key)
            {
                return (TValue)cache.GetOrDefault(key.ToString());
            }
    

    This code can throw an exception if cache or key is null.

  • User Avatar
    0
    theedge created

    Hi Halil,

    My key is not null as far as I am aware. My cache is empty and I am supplying a key value from my passed in value of aParams.BrandSeoFriendly which is the string "celebrity-cruises".

    So is it possible that if there is no value associated with the key that the hard cast of:

    (TValue)cache.GetOrDefault(key.ToString())
    

    is failing? Wouldn't soft cast of:

    cache.GetOrDefault(key.ToString()) as (TValue)
    

    be better? It seems to me that this issue is because the associated value is actually an INT.

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I think this implementation is better: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp/Runtime/Caching/CacheExtensions.cs#L52">https://github.com/aspnetboilerplate/as ... ons.cs#L52</a>

    public static TValue GetOrDefault<TKey, TValue>(this ICache cache, TKey key)
            {
                var value = cache.GetOrDefault(key.ToString());
                if (value == null)
                {
                    return default(TValue);
                }
    
                return (TValue) value;
            }
    

    Will be released in next version.

    But note that: Do not store null values in the cache since MemoryCache does not allow null values.

  • User Avatar
    0
    theedge created

    Hi Halil,

    I am not storing NULL values in the cache. The exception is being thrown when their is no match in the cache. Not sure if it has to do with the fact that the value type of int can be NULL?

    For what it is worth I modified my code (unsing 0.7.4.1) to use Get() instead of GetOrDefault(), leaving everything else unchanged and the exception went away.