1

AzureでホストされているWebアプリケーションからこのエラーが発生しています。それは数日はOKを実行し、その後突然動作を停止します。EFコア:クエリの結果を反復処理中にデータベースで例外が発生しました

おそらく接続に問題はありますか?どのオブジェクト参照にインスタンスがないのですか?

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

私は自分のリポジトリメソッドを同期させ、コントローラで非同期にすることができますか?

+1

接続文字列を指定できますか?たぶんあなたはそれに "MultipleActiveResultSets = true"を追加する必要があります – hugoterelle

+0

それはfalseです。私はそれを真実に設定し、試してみましょう。ありがとう。 –

+0

Trueに設定して実行させると、再び動作します。数日後、別のエラーが表示されます。「接続でMultipleActiveResultSetsがサポートされていません。」私はそれが何回か働いたときになぜそれを得ているのか分かりません。 –

答えて

0

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を確認することができます。

第2に、要求ごとにコンテキストインスタンスを作成/使用することができます。this artciel shows some general guidelines when deciding on the lifetime of the contextをチェックすることができます。

+0

私はhugorgorの提案通りにMARSをtrueに設定しようとします。 DbContextを注入したいので、リクエストごとにインスタンスを実行することはできません。要求ごとに行うことはできますが、依然としてリポジトリの外のどこかから注入することは可能ですか? –

+0

Dependency injection frameworkを使用しているようですが、DIコンテナを使用してDbContextインスタンスにPerWebRequestの有効期間を挿入しようとしてください。 –

+0

Transientはデフォルトの有効期間ではありませんか?これはWebリクエストごとに相当すると思います。 –

関連する問題