2009-08-21 2 views
0

asp.net MVC 응용 프로그램에서 작업하고 있습니다.MethodInfo.Invoke가 때때로 null을 반환하고 값을 반환하는 경우가 있습니다.

간단한 linq 문을 사용하여 db에서 데이터를 가져 오는 저장소를 래핑하는 클래스가 있습니다. 캐싱 로직 (캐싱 애플리케이션 블록 사용)을 추가하기 위해 데코레이터 클래스를 작성했습니다.

내가 장식하고 싶은 몇 가지 메소드가 있기 때문에 논리는 모두 동일합니다 (캐시에 실제 getter와 store를 호출하지 않으면 캐시에 있는지 확인하십시오). 다음과 같이 씁니다 :

등 캐시에 존재 해 확인하는 일반적인 논리를 수행하는 도우미 메서드 :

public object CachedMethodCall(MethodInfo realMethod, params object[] realMethodParams) 
    { 
     object result = null; 
     string cacheKey = CachingHelper.GenereateCacheKey(realMethod, realMethodParams); 

     // check if cache contains key, if yes take data from cache, else invoke real service and cache it for future use. 
     if (_CacheManager.Contains(cacheKey)) 
     { 
      result = _CacheManager.GetData(cacheKey); 
     } 
     else 
     { 
      result = realMethod.Invoke(_RealService, realMethodParams); 

      // TODO: currently cache expiration is set to 5 minutes. should be set according to the real data expiration setting. 
      AbsoluteTime expirationTime = new AbsoluteTime(DateTime.Now.AddMinutes(5)); 
      _CacheManager.Add(cacheKey, result, CacheItemPriority.Normal, null, expirationTime); 
     } 

     return result; 
    } 

모두가 잘 사랑스러운 작동이.

StackTrace currentStack = new StackTrace(); 
string currentMethodName = currentStack.GetFrame(0).GetMethod().Name; 
var result = (GeoArea)CachedMethodCall(_RealService.GetType().GetMethod(currentMethodName), someInputParam); 
return result; 

문제가 가끔 realMethod.Invoke (...)이 일어나고 라인이 null을 반환한다는 것입니다 : 각 장식 방법에 나는 다음과 같은 코드가 있습니다. 바로 다음에 중단 점을 놓은 다음 해당 행으로 실행을 반환하면 결과가 null이 아니며 데이터가 DB에서 반입됩니다. 모든 입력 변수가 정확하고, DB에 데이터가 존재하며, 두 번째 실행에서 데이터를 얻습니다. 따라서 첫 번째 실행에서 어떤 문제가 발생합니까?!

public object CachedMethodCall(MethodInfo realMethod, params object[] realMethodParams) 
    { 
     string cacheKey = CachingHelper.GenereateCacheKey(realMethod, realMethodParams); 

     object result = _CacheManager.GetData(cacheKey); 

     if (result == null) 
     { 
      result = realMethod.Invoke(_RealService, BindingFlags.InvokeMethod, null, realMethodParams, CultureInfo.InvariantCulture); 

      // TODO: currently cache expiration is set to 5 minutes. should be set according to the real data expiration setting. 
      AbsoluteTime expirationTime = new AbsoluteTime(DateTime.Now.AddMinutes(5)); 
      _CacheManager.Add(cacheKey, result, CacheItemPriority.Normal, null, expirationTime); 
     } 

     return result; 
    } 

내가 이전 _CacheManager.Contains 호출이 때로는 캐시 true를 반환 한 것으로 나타났습니다 :

감사합니다 :)

답변

0

나는 다음과 같이 내가 코드를 업데이트하여이 문제를 해결하기 위해 관리 생각 데이터를 포함하지 않았습니다. 문제를 일으키는 스레드가 의심 스럽지만 확실하지 않습니다 ...