NullReferenceException while get Data from Memcached (.NET)

683 Views Asked by At

Have problem while getting data from Memcached on .NET MVC solution.

I have this custom repository:

public List<DropDownLocalization> GetLocalization(string key, string lang)
        {
            var result = cacheClient.Get<IQueryable<DropDownLocalization>>("DD_" + key + "_" + lang);
            if (result == null)
            {
                int getLangId = _db.languages.Where(d => d.Association == lang).Select(d => d.Id).FirstOrDefault();
                int getLableItemId = _db.lables_dropdown.Where(d => d.Key == key).Select(d => d.Id).FirstOrDefault();
                var get = _db.lables_dropdown_items.Where(d => d.LableId == getLableItemId).Select(d => new DropDownLocalization
                                                    {
                                                        DDId = d.Id,
                                                        DDName = d.lables_dropdown_values.Where(m => m.Language == getLangId).Select(m => m.Value).FirstOrDefault()
                                                    }).AsQueryable();
                cacheClient.Store(StoreMode.Add, "DD_" + key + "_" + lang, (IQueryable<DropDownLocalization>)get);
                EFQueryLogger.WriteQuery(((ObjectQuery)get).ToTraceString());
                return get.ToList();
            }
            return result.ToList();
        }

It's get list of drop down labels localized from current lang.

So it's trying to get data from cache, if not success put data to cache. While i comment part of code with "if" statement (just to see if it's get data data from cache) i have error of null reference. It's mean that request response is not in the cache.

Can somebody put my nose to the problem?

There is my enym client library config:

<enyim.com>
    <memcached protocol="Text">
      <servers>
                <add address="localhost" port="11211" />
      </servers>
      <socketPool deadTimeout="00:00:10" />
    </memcached>
  </enyim.com>
2

There are 2 best solutions below

0
On BEST ANSWER

Solution found. Unfortunately it was my mistakes. There is the practice how to store data model in memcached.

I have model:

[Serializable]
    public class DropDownLocalization
    {
        public int DDId { get; set; }
        public string DDName { get; set; }
    }

And method that return result:

public List<DropDownLocalization> GetLocalization(string key, string lang)
        {
           List<DropDownLocalization> result = MemcachedSingleton.Instance.Get<List<DropDownLocalization>>("DD_" + key + "_" + lang);
           if (result == null)
           {
               int getLangId = _db.languages.Where(d => d.Association == lang).Select(d => d.Id).FirstOrDefault();
               int getLableItemId = _db.lables_dropdown.Where(d => d.Key == key).Select(d => d.Id).FirstOrDefault();
               result = _db.lables_dropdown_items.Where(d => d.LableId == getLableItemId).Select(d => new DropDownLocalization
                                                   {
                                                       DDId = d.Id,
                                                       DDName = d.lables_dropdown_values.Where(m => m.Language == getLangId).Select(m => m.Value).FirstOrDefault()
                                                   }).ToList();
               MemcachedSingleton.Instance.Store(StoreMode.Add, "DD_" + key + "_" + lang, result);
               return result;
           }
           return result;
        }

It's fully working now.

Config should be next:

<enyim.com>
    <memcached protocol="Text">
      <servers>
        <add address="localhost" port="11211" />
      </servers>
      <transcoder type="Enyim.Caching.Memcached.DataContractTranscoder, Enyim.Caching" />
      <socketPool deadTimeout="00:00:10" />
    </memcached>
  </enyim.com>
0
On
  • Print your mappings and entities please.
  • LazyLoad and ProxyGeneration are enabled?
  • Did you test it from several AppDomains?

test case:

  1. AppDomain "A" added to memcached serialized EF 4.1 Code First entity "e1" (dynamic proxy type from "A" scope)
  2. AppDomain "B" get from memcached entity "e1" and try to desirialize it

AssertException

Could not load file or assembly 
'EntityFrameworkDynamicProxies-***.DomainModel,
 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
 or one of its dependencies. The system cannot find the file specified.