1

我们在一个刚刚上线的项目中使用 EntityFramework。根据我们的日志,有几个用户正在遭受“无效操作。连接已关闭”,每天大约发生 50 次。

异常看起来像:

An error occurred while executing the command definition. See the inner exception for details.    at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at xxxx.SelectResultToPeople(IEnumerable`1 result) 
   at xxxx.GetResult()
   at DynamicModule.ns.Wrapped_IPeopleGetter_a853d13914444ca6ab0e016e035520c5.<People_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
Invalid operation. The connection is closed.    at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
   at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
   at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
   at System.Data.SqlClient.TdsParserStateObject.TryReadByte(Byte& value)
   at System.Data.SqlClient.TdsParserStateObject.TryPeekByte(Byte& value)
   at System.Data.SqlClient.SqlDataReader.TrySetMetaData(_SqlMetaDataSet metaData, Boolean moreInfo)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryCloseInternal(Boolean closeReader)
   at System.Data.SqlClient.SqlDataReader.Close()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

此异常会在非常特定的页面上引发,因此它不会随机分布在应用程序中。其中一个页面使用表函数通过实体框架对数据库进行简单查询。表函数名为“GetPeopleNames”,我们的 ObjectContext 类是名为“SiteEntities”的类。

public class PeopleGetter : IPeopleGetter
{
    public List<object> GetResult()
    {
        using (var siteEntities = new SiteEntities())
        {
            IQueryable<someResultEntity> result = 
                 siteEntities.GetPeopleNames()
                   .SortBy("Name", "asc")
                   .Skip(20*query.CurrentPagingPosition)
                   .Take(20);


            List<People> people = SelectResultToPeople(result)
            return people;

        }
    }

    private List<People> SelectResultToPeople(IQueryable<someResultEntity> result)
    {
        return result
               .ToList()
               .Select(x => new People { })
               .ToList();
    }
}

我不明白为什么它应该与我们的错误有关,但我最好提一下 PeopleGetter 类是由 Unity IoC 容器解析和创建的。PeopleGetter 注册了一个 'PerRquest' LifeTimeManager,如下所示:

public class PerCallContextLifeTimeManager : HierarchicalLifetimeManager
{
    private readonly string _key =  string.Format("PerCallContextOrRequestLifeTimeManager_{0}", Guid.NewGuid());

    public override object GetValue()
    {
        if (HttpContext.Current != null)
            return GetFromHttpContext();
        else
            return base.GetValue();
    }

    private object GetFromHttpContext()
    {
        return HttpContext.Current.Items[_key];
    }

    public override void SetValue(object newValue)
    {
        if (HttpContext.Current != null)
            SetInHttpContext(newValue);
        else
                    base.SetValue(newValue);
    }

    private void SetInHttpContext(object newValue)
    {
            HttpContext.Current.Items.Add(_key, newValue);
    }


    public override void RemoveValue()
    {
    }
}

任何想法为什么我们要关闭连接?

谢谢你的帮助!

4

0 回答 0