Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Invoke有时返回null,有时返回value_C#_Asp.net Mvc_Reflection_Multithreading_Methodinfo - Fatal编程技术网

C# Invoke有时返回null,有时返回value

C# Invoke有时返回null,有时返回value,c#,asp.net-mvc,reflection,multithreading,methodinfo,C#,Asp.net Mvc,Reflection,Multithreading,Methodinfo,我正在开发一个asp.net MVC应用程序 我有一个类,它封装了一个存储库,使用简单的linq语句从数据库中获取数据。我已经编写了一个decorator类来使用缓存应用程序块添加缓存逻辑 因为我有几个方法要修饰,每个方法的逻辑都是一样的,检查是否存在于缓存中,如果不调用真正的getter并存储在缓存中,我写了如下内容: 执行检查缓存中是否存在等常见逻辑的帮助器方法: public object CachedMethodCall(MethodInfo realMethod, params

我正在开发一个asp.net MVC应用程序

我有一个类,它封装了一个存储库,使用简单的linq语句从数据库中获取数据。我已经编写了一个decorator类来使用缓存应用程序块添加缓存逻辑

因为我有几个方法要修饰,每个方法的逻辑都是一样的,检查是否存在于缓存中,如果不调用真正的getter并存储在缓存中,我写了如下内容:

执行检查缓存中是否存在等常见逻辑的帮助器方法:

    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,并从数据库中获取数据。所有的输入变量都是正确的,数据存在于数据库中,第二次运行得到数据,那么第一次运行出了什么问题


谢谢:

我想我通过如下更新代码成功地解决了这个问题:

    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,即使缓存中不包含数据。我怀疑线程导致了问题,但我不确定