When does a dictionary throw an IndexOutOfRangeException on Add or ContainsKey?(字典何时在 Add 或 ContainsKey 上引发 IndexOutOfRangeException?)
问题描述
在一个繁忙的 ASP .NET 网站上,我有一个字典,它充当缓存,基本上存储键/值对以供以后检索.
On a busy ASP .NET website, I have a Dictionary, which acts as a cache, basically storing key/value pairs for later retrieval.
在高负载时,字典有时会进入一种状态,每当我调用 ContainsKey 或 Add 方法时,它总是抛出 IndexOutOfRangeException.异常发生在私有 FindEntry 方法中.
On high load, the Dictionary some times get into a state, where it always throws an IndexOutOfRangeException whenever i call the ContainsKey or Add method. The exception happens inside the private FindEntry method.
我怀疑这可能是由于同步问题,但我不确定.
I am suspecting that this might be due to a synchronization issue, but I am not sure.
谁能告诉我在什么情况下会发生这种情况?我的目标是收集足够的信息,以便我可以在开发环境中重现该问题.
Can anyone tell me under which circumstances this can happen ? My goal is to gather enough information so that I can reproduce the issue in the dev environment.
推荐答案
字典 状态:
字典
A Dictionary<TKey,TValue> can support multiple readers concurrently, as long as the collection is not modified. ... To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
如果您没有同步访问 Dictionary
,那么您可能会遇到您所描述的问题(可能是因为内部状态不再有效).如果您想尝试在您的开发环境中重现这一点,请尝试创建一个程序,该程序使用多个线程在不同步的情况下从 Dictionary
连续读取和写入.
If you are not synchronizing access to the Dictionary
, then you might get issues like those you describe (presumably because the internal state is no longer valid). If you want to try and reproduce this in your development environment, then try creating a program that uses multiple threads to continuously read and write from a Dictionary
without synchronization.
这篇关于字典何时在 Add 或 ContainsKey 上引发 IndexOutOfRangeException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:字典何时在 Add 或 ContainsKey 上引发 IndexOutOfRangeException?
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30