Ok first first. This is the exception information provided by the support team. I know the line and code where this happens. This happens when calling FirstOrDefault on a dictionary retrieved from the cache.
1) Exception Information
*********************************************
Exception Type: System.InvalidOperationException
Message: Collection was modified; enumeration operation may not execute.
Data: System.Collections.ListDictionaryInternal
Now I wanted to model the problem, and I could do it in a simple ASP.net application.
My page has 2 buttons - Button_Process and Button_Add.
The code is as follows:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var data = Cache["key"];
if (data == null)
{
var dict = new Dictionary<int, string>();
for (int i = 0; i < 10; i++)
{
dict.Add(i, "i");
}
Cache["key"] = dict;
}
}
}
protected void ButtonProcess_Click(object sender, EventArgs e)
{
var data = Cache["key"] as Dictionary<int, string>;
if (data != null)
{
foreach (var d in data.Values)
{
Thread.Sleep(1000);
if (d.Contains("5"))
{
}
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
var data = Cache["key"] as Dictionary<int, string>;
if (data != null)
{
data.Add(new Random().Next(), "101");
Cache["key"] = data;
}
}
}
Now suppose there are 2 queries:
1. - button_Process,
2. - button_Add, - blah blah
- , . :
1. for ( FirstOrDefault ). , . - ,
2. - , , .
, . . ?