asp.net-web-api - EF 核心 : An exception occurred in the database while iterating the results of a query

标签 asp.net-web-api azure-sql-database azure-web-app-service entity-framework-core

我从 Azure 中托管的 Web 应用程序中收到此错误。它运行了几天,然后突然停止工作。

可能是连接有问题?哪个对象引用没有实例?

YYYY-MM-DD hh:mm:ss [Error] An exception occurred in the database while iterating the results of a query.
System.NullReferenceException: Object reference not set to an instance of an object.    at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.<BufferAllAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.GetResult()    at Microsoft.EntityFrameworkCore.Query.RelationalQueryContext.<RegisterValueBufferCursorAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.GetResult()    at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.<BufferlessMoveNext>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()  at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.<MoveNext>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.SelectAsyncEnumerable`2.SelectAsyncEnumerator.<MoveNext>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()  at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.<_FirstOrDefault>d__82`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()  at Microsoft.EntityFrameworkCore.Query.Internal.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()  at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.SelectAsyncEnumerable`2.SelectAsyncEnumerator.<MoveNext>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()  at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()  at MyProject.Data.SectionRepository.<GetByTypePathAndAlias>d__6.MoveNext() in /Users/webaccount/myproject/src/MyProject.Data/Repositories/SectionRepository.cs:line 34

我的存储库或实体代码没有什么特别之处。
public async Task<Section> GetByTypeId(string typeId) 
{
    var sections = from s in this.DbContext.Sections
        where s.TypeId == typeId
        select s;

    return await sections.FirstOrDefaultAsync(); // <-- LINE 34
}

这是我的实体。
public class Section
{
        [Key]
        public int Id { get; set; }
        public DateTimeOffset CreatedDate { get; set; }
        public string TypeId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
}  

或许与此有关? https://github.com/aspnet/EntityFrameworkCore/issues/8026

如果我将我的存储库方法转换为同步并在 Controller 上保持异步会有所帮助吗?

最佳答案

It runs ok for a few days and then suddenly stops working.

Object reference not set to an instance of an object


首先,您可以尝试remote debug your web app并检查 this.DbContext .
其次,您可以为每个请求创建/使用上下文实例,以及 this artciel shows some general guidelines when deciding on the lifetime of the context ,你可以检查一下。

关于asp.net-web-api - EF 核心 : An exception occurred in the database while iterating the results of a query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45969939/

相关文章:

c# - Web API 服务在读取流时挂起

azure - Azure sql DB 中的最大池大小与并发登录数

azure-sql-database - 使用 Azure Data Studio 的适当角色和权限

azure - 如何在Azure应用程序洞察中查找实例区域

azure - 我应该为 ASP.NET Core 应用程序选择哪个 Azure 应用服务?

asp.net-web-api - 如何以字典的形式访问所有查询字符串参数

azure - SQL Azure 数据库没有基本/标准/高级选项

sql - 创建 Windows Azure Sql 表

azure - 如何将新文件路径添加到azure应用程序服务中的环境变量Path

c# - 返回文件结果时出现 HTTP ERROR 500