【发布时间】:2018-08-09 07:56:09
【问题描述】:
我的 WCF 服务中有以下代码,它正在抛出 System.IndexOutOfRangeException
Dictionary<string, List<ChunkData>> chunkDataTable = cacheManager[cacheKey] as Dictionary<string, List<ChunkData>>;
if (!chunkDataTable.Keys.Contains(ticker))
{
chunkDataTable.Add(ticker, new List<ChunkData>());
}
List<ChunkData> listOfChunks = new List<ChunkData>();
ChunkData lastIncompeleteChunk = null;
if (chunkDataTable[ticker].Count > 0)
{
Logger.Write("Log one"); //I see this logs
if (reqEndDate <= chunkDataTable[ticker].Last().EndDate)
{
//meaning we can directly take last chunk (which can be incompelete too) directly as we dont need to add further data into for now
listOfChunks = chunkDataTable[ticker].Where
(chunk =>
((chunk.StartDate >= reqStartDate && chunk.EndDate <= reqEndDate) || //whole chunks: which lies in between of requested start and end date
(chunk.StartDate <= reqStartDate && reqStartDate <= chunk.EndDate) || //Left Most Chunk: if req start date lies within some chunk (between start and end date of chunk)
(chunk.StartDate <= reqEndDate && reqEndDate <= chunk.EndDate) //Right Most Chunk: if req end date lies within some chunk (between start and end date of chunk)
)).OrderBy(x => x.EndDate).ToList();
}
else
{
listOfChunks = chunkDataTable[ticker].Where
(chunk => !chunk.IsIncomplete &&
((chunk.StartDate >= reqStartDate && chunk.EndDate <= reqEndDate) || //whole chunks: which lies in between of requested start and end date
(chunk.StartDate <= reqStartDate && reqStartDate <= chunk.EndDate) || //Left Most Chunk: if req start date lies within some chunk (between start and end date of chunk)
(chunk.StartDate <= reqEndDate && reqEndDate <= chunk.EndDate) //Right Most Chunk: if req end date lies within some chunk (between start and end date of chunk)
)).OrderBy(x => x.EndDate).ToList();
if (chunkDataTable[ticker].Last().IsIncomplete)
lastIncompeleteChunk = chunkDataTable[ticker].Last();
}
if (listOfChunks != null)
Logger.Write("Line two"); //I don't see this log
}
reqEndDate 和 reqStartDate 来自客户端计算机,根据日志,它们是正确的。
cacheManager 是微软企业库的缓存管理器
完整的堆栈跟踪:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Linq.Enumerable.Last[TSource](IEnumerable`1 source)
不知道什么时候会出现这种异常
【问题讨论】:
-
异常是因为
chunkDataTable[ticker]而不是因为.Last() -
@un-lucky 你的意思是
chunkDataTable可能不包含'ticker'?,如果是这样:但我可以获得第一个日志“log one” -
是一直扔还是不时扔?
cacheManager是静态的吗? -
@Evk 是的,它每次都会针对特定的
ticker抛出,没有 cacheManager 不是静态的,但它为每个请求都持有公共缓存。因为它在生产中,所以我不能直接调试它。 -
我会说这是线程问题。另一个线程(例如另一个 WCF 请求)在您检查计数后修改
chunkDataTable[ticker]集合(甚至在您的Last()语句中间)。
标签: c# linq wcf indexoutofboundsexception generic-list