Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# Linq Where子句最终导致NullReferenceException_C#_.net_Linq_Iis - Fatal编程技术网

C# Linq Where子句最终导致NullReferenceException

C# Linq Where子句最终导致NullReferenceException,c#,.net,linq,iis,C#,.net,Linq,Iis,我有一个在IIS托管的网站。我在存储库中缓存值,以避免多次访问数据库。缓存在repo的顶部实例化,如下所示: protected static List<MaxDeposit> _cached_values = new List<MaxDeposit>(); protected static List_cached_values=new List(); 当调用从数据库检索数据时,我首先检查缓存,看看是否已经有了它: protected List<MaxDepos

我有一个在IIS托管的网站。我在存储库中缓存值,以避免多次访问数据库。缓存在repo的顶部实例化,如下所示:

protected static List<MaxDeposit> _cached_values = new List<MaxDeposit>();
protected static List_cached_values=new List();
当调用从数据库检索数据时,我首先检查缓存,看看是否已经有了它:

protected List<MaxDeposit> GetMaxDepositsFromCacheByTier(int tierId)
{
    return _cached_values.Where(x => x.TierId == tierId).ToList();
}
受保护列表GetMaxDepositsFromCacheByter(int-tierId)
{
返回_缓存的_值。其中(x=>x.TierId==TierId.ToList();
}
这将按预期工作,并返回一个或多个值,或一个空列表。如果返回空列表,则从数据库中检索数据,然后将其添加到缓存中,如下所示:

protected void AddMaxDepositsToCache(List<MaxDeposit> maxDeposits)
{
     if (maxDeposits == null) return;

     lock (this)
     {
        _cached_values.AddRange(maxDeposits);
     }
 }
受保护的void addMaxDepositoCache(列出maxDeposits)
{
如果(maxDeposits==null)返回;
锁(这个)
{
_缓存的_值。AddRange(最大存款);
}
}
这正在生产中,在返回语句中出现NullReferenceExceptions之前的几天内,它似乎工作正常:

System.NullReferenceException: Object reference not set to an instance of an object.
   at MyProject.Model.Data.Repositories.MaxDepositRepository.<>c__DisplayClass3_0.<GetMaxDepositsFromCacheByTier>b__0(MaxDeposit x) in Data\Repositories\MaxDepositRepository.cs:line 31
   at System.Linq.Enumerable.WhereListIterator<T>.MoveNext()
   at System.Collections.Generic.List<T>..ctor(IEnumerable<T> collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable<T> source)
   at MyProject.Model.Data.Repositories.MaxDepositRepository.GetMaxDepositsFromCacheByTier(Int32 tierId) in Data\Repositories\MaxDepositRepository.cs:line 31
   at MyProject.Model.Data.Repositories.MaxDepositRepository.GetByTier(Int32 tierId) in Data\Repositories\MaxDepositRepository.cs:line 0
System.NullReferenceException:对象引用未设置为对象的实例。
在Data\Repositories\MaxDepositoryRepository.cs中的MyProject.Model.Data.Repositories.MaxDepositoryRepository.c_uu显示Class3_0.b_uu0(MaxDeposit x):第31行
在System.Linq.Enumerable.WhereListIterator.MoveNext()中
位于System.Collections.Generic.List..ctor(IEnumerable集合)
在System.Linq.Enumerable.ToList[TSource](IEnumerable source)
在Data\Repositories\MaxDepositRepository.cs中的MyProject.Model.Data.Repositories.MaxDepositRepository.GetMaxDepositsFromCacheByTier(Int32 tierId)中:第31行
在Data\Repositories\MaxDepositRepository.cs中的MyProject.Model.Data.Repositories.MaxDepositRepository.GetByTier(Int32 tierId)中:第0行
一旦发生这种情况,对该代码的每次调用都会引发相同的异常。回收应用程序池可解决此问题,并再次成功进行调用

我假设_缓存的_值最终会变成NULL(即使在代码中没有设置为NULL),因此我修改了代码:

protected List<MaxDeposit> GetMaxDepositsFromCacheByTier(int tierId)
        {
            if (_cached_values == null)
            {
                _cached_values = new List<MaxDeposit>();

                _logger.LogInformation($"MaxDepositRepository.GetMaxDepositsFromCacheByTier(int tierId): _cached_values field was null. tierId = {tierId}");
            }

            return _cached_values.Where(x => x.TierId == tierId).ToList();
        }
受保护列表GetMaxDepositsFromCacheByter(int-tierId)
{
如果(_缓存的_值==null)
{
_缓存的_值=新列表();
_logger.LogInformation($“MaxDepositRepository.GetMaxDepositsFromCacheByTier(int tierId):\u cached_values字段为空。tierId={tierId}”);
}
返回_缓存的_值。其中(x=>x.TierId==TierId.ToList();
}
所以现在我在使用它之前检查缓存的空值,如果它为空,则实例化一个新的空列表,并记录我们必须这样做。这种变化正在生产中,但同样的问题昨天再次发生。没有找到添加的日志记录,这告诉我_缓存的_值在任何时候都不是NULL

问题与Linq Where子句有关吗?如果是这样的话,为什么它会在崩溃前工作一段时间


如果有人能够解释这里发生了什么或指出我哪里出了错,请提前感谢

如果
\u cached\u values
中的单个项为
null
,会发生什么情况?如果
x
null
,则无法获取
TierId
的值,对吗?另外,您说过这是一个web应用程序<代码>列表不是线程安全的,所以如果您从多个不同的线程更改它,可能会发生奇怪的事情。我们并行地将项目添加到列表中,忽略任何错误,尽管从未添加任何
null
对象,但我们最终得到了许多
null
条目。由于NRE映射在
x=>x.TierId==TierId
委托中,几乎可以肯定
x
中存在null。@Llama感谢这个例子,我想这一定是出了问题。@canton7是的,谢谢,我想你是对的。