ToListAsync引发无法访问已处置的对象

时间:2018-06-28 01:06:18

标签: c# entity-framework async-await

有人知道为什么ToListAsync会引发错误:

  

“ System.ObjectDisposedException:无法访问已处置的对象”

使用以下代码?

using (var c = new Context())
{
    var companies = await c.Database.SqlQuery<Company>("exec 
            GetStoredPrcedure @id", new SqlParameter("@id", id))
        .ToListAsync();

    // Process companies.
}

这是完整的堆栈跟踪。

  

System.ObjectDisposedException:无法访问已处置的对象。在System.Reflection.RuntimeAssembly._nLoad(AssemblyName文件名,字符串codeBase,证据assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark&stackMark,IntPtr pPrivHostBinder,布尔throwOnFileNotFound,布尔for Introspection,布尔抑制自检)   在System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef,证据assemblySecurity,RuntimeAssembly reqAssembly,StackCrawlMark&stackMark,IntPtr pPrivHostBinder,布尔throwOnFileNotFound,   在System.Data.Entity.Core.Metadata.Edm.MetadataAssemblyHelper处,在System.Reflection.Assembly.Load(AssemblyName assemblyRef)处为Boolean forIntrospection,布尔抑制类Checks。   System.Data.Entity.Core.Metadata.Edm.MetadataAssemblyHelper.d__0.MoveNext()上的SafeLoadReferencedAssembly(AssemblyName assemblyName)   在System.Linq.Enumerable.Any [TSource](IEnumerable'1源,Func'2谓词)处,在System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.ImplicitLoadAssemblyForType(Type type,Assembly CallingAssembly)   在System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.TryDetermineCSpaceModelType(Type type,EdmType&modelEdmType)   在System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndClrType(DbDataReader reader,Type type,MetadataWorkspace工作区)   在System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate [TElement]处(DbDataReader阅读器,字符串entitySetName,MergeOption mergeOption,布尔流,EntitySet和entitySet,TypeUsage和edmType)   在System.Data.Entity.Core.Objects.ObjectContext.d__73'1.MoveNext()---从上一个引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在System.Data.Entity.Core.Object.ObjectContext.d__3d'1.MoveNext()在System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter'1.GetResult()   ---从之前抛出异常的位置开始的堆栈跟踪---位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.d__9'1.MoveNext()处的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   ---从之前抛出异常的位置开始的堆栈跟踪---位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter'1.GetResult()的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)处   在System.Data.Entity.Core.Objects.ObjectContext.d__70'1.MoveNext()-从发生异常的上一位置开始的堆栈跟踪结束-   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在System.Data.Entity.Internal.LazyAsyncEnumerator'1.d__0.MoveNext()的System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter'1.GetResult()处   ---从之前抛出异常的位置开始的堆栈跟踪---位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.d__5'1.MoveNext()处的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   ---从先前抛出异常的位置开始的堆栈跟踪---位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上,在Services.Companies.CompanyService.d__41.MoveNext()   在API \ src \ Services \ Companies \ CompanyService.cs:line 927

只有在服务器流量增加后,该异常才会出现。我怀疑GC会在返回整个结果集之前处理掉该连接,但是我不确定为什么或我对此有什么办法。

0 个答案:

没有答案