IndexOutOfRangeException上Queryable.Single [英] IndexOutOfRangeException on Queryable.Single

查看:201
本文介绍了IndexOutOfRangeException上Queryable.Single的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经很长一段时间完美运行ASP.NET网站,什么也没有最近更改。从一小时,接下来我就开始在一条线在那里我做了LINQ查询这样收到IndexOutOfRangeException:

I have an ASP.NET site that has been running perfectly for a long time, nothing's changed recently. From one hour to the next I started receiving an IndexOutOfRangeException in a line where I do a LINQ query like this:

var form = SqlDB.GetTable<ORMB.Form, CDB>()
    .Where(f => f.FormID == formID)
    .Single();



ORMB.Form是LINQ to SQL的一个POCO对象的属性将其映射到一个MSSQL表(映射验证为正确的)。堆栈跟踪如下:

ORMB.Form is a POCO object with LINQ to SQL attributes mapping it to an MSSQL table (mapping is verified as correct). The stacktrace is as follows:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.List`1.Add(T item)
   at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user)
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.Single[TSource](IQueryable`1 source)
   at GetForm.Page_Load(Object sender, EventArgs e)

System.Collections中反思.Generic.List.Add显示下面的代码:

Reflecting System.Collections.Generic.List.Add shows the following code:

public void Add(T item)
{
    if (this._size == this._items.Length)
    {
        this.EnsureCapacity(this._size + 1);
    }
    this._items[this._size++] = item;
    this._version++;
}

这应该是容易出现IndexOfOutRangeException唯一的线是this._items [本._size ++] =项目,我看不出我怎么然而影响到这一点。

The only line that should be prone to the IndexOfOutRangeException is this._items[this._size++] = item, I cannot see how I'm affecting this however.

我可以做一个AppDomain回收解决问题,所以它必须缓存是某种联系。 ObjectTracking被关闭的DataContext的,在这种重要的情况下。

I can solve the problem by doing an appdomain recycle, so it must be caching related somehow. ObjectTracking is turned off on the DataContext, in case that matters.

我的直觉是,这可能是一个线程问题,SqlConnectionManager已经在叫列表字段缓存IConnectionUsers用户。如果两个线程在同一时间进入Add方法,是什么阻止以下情况发生:

My gut feeling is that this might be a threading issue, SqlConnectionManager having cached IConnectionUsers in the List field called 'users'. If two threads enter the Add method at the same time, what prevents the following from happening:

T1: Add(x)
T2: Add(y)
T1: Since _size == _items.Length: EnsureCapacity(_size + 1)
T2: Since _size > _items.Length: _items[_size++] = item;
T1: _items[size++] = item <- OutOfRangeException since T2 didn't increase the capacity as needed

有人吗?

推荐答案

您共享一个共同的DataContext?这可以解释你所描述,因为DataContext的不是线程安全的线程问题。

Are you sharing a common DataContext? That would explain the threading issues you are describing, as DataContext is not thread safe.

这篇关于IndexOutOfRangeException上Queryable.Single的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆