4

When calling ToList() after invoking the compiled query I sometimes(!) receive an exception - System.InvalidOperationException: Invalid attempt to call Read when reader is closed.

The DB context is created for every "invoke" calling, the queries are synchronously compiled before the invocation. The snippet of code is called one time pro file , which are being read asynchronously.

The Query is initialised, compiled and invoked the following way.

public Func<SANAContext, int, string, IEnumerable<KeyValuePair<string, Importartikelhash>>> ImportingArticlesHashesQuery;

...

ImportingArticlesHashesQuery = EF.CompileQuery((SANAContext db, int LaLiNr, string lagerId) => db.Importartikelhash.AsNoTracking()
            .Where(la => la.LieferantartikelId == LaLiNr && string.Compare(la.LagerId, lagerId, StringComparison.Ordinal) == 0).Select(a => new KeyValuePair<string, Importartikelhash>(a.LieferartikelNr, a)));

...    

List<KeyValuePair<string, Importartikelhash>> records = ImportingArticlesHashesQuery.Invoke(context, supplier.SupplierInternalId, warehouse.Id).ToList();  

On a big amount of data i receive an exception :

   {System.InvalidOperationException: Invalid attempt to call Read when reader is closed.
   at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)
   at System.Data.SqlClient.SqlDataReader.Read()
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Sanalogic.SupplierArticlesImport.SupplierDataParserApplication.Services.QueriesCompiler.DBHashValuesCache(PricatSupplier supplier, PricatWarehouse warehouse) in QueriesCompiler.cs:line 90}

What can cause the problem? I realize that some problem with asynchrones operations exists, but I don't know where to search for it.

Any help is highly appreciated!

mmam
  • 86
  • 6

1 Answers1

0

Could you please try to increase the command timeout on your context like the following :

    public class MyDatabase : DbContext
{
    public MyDatabase ()
        : base(ContextHelper.CreateConnection("Connection string"), true)
    {
        ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 180;
    }
}
  • I also thought about it , but i have already set TimeOut to a pretty big value (500) – mmam Mar 26 '19 at 15:03