BlueshiftSoftware / EntityFrameworkCore

NoSQL providers for EntityFramework Core
Other
281 stars 57 forks source link

Get ObjectDisposedException when execute same query on differend DbContext instances #41

Open IvoryRubble opened 5 years ago

IvoryRubble commented 5 years ago

Hello. I always get ObjectDisposedException when execute same query on differend instances of dbContext. Firs execution is succesfull but second execution on different instance of dbContext fails with ObjectDisposedException: System.ObjectDisposedException : Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.

Here the test case to reproduse this issue:

        [Fact]
        public async Task Can_execute_same_async_query_from_different_dbContext_instances()
        {
            await ExecuteUnitOfWorkAsync(async zooDbContext =>
            {
                zooDbContext.Animals.AddRange(_zooEntities.Animals);
                Assert.Equal(
                    _zooEntities.Entities.Count,
                    await zooDbContext.SaveChangesAsync(acceptAllChangesOnSuccess: true));
            });

            Func<ZooDbContext, Task<List<Animal>>> query = (zooDbContext) => 
            zooDbContext.Animals
                .OrderBy(animal => animal.Name)
                .ThenBy(animal => animal.Height)
                .ToListAsync();

            await ExecuteUnitOfWorkAsync(async zooDbContext =>
            {
                Assert.Equal(_zooEntities.Animals,
                    await query.Invoke(zooDbContext),
                    new AnimalEqualityComparer());
            });

            await ExecuteUnitOfWorkAsync(async zooDbContext =>
            {
                Assert.Equal(_zooEntities.Animals,
                    await query.Invoke(zooDbContext),
                    new AnimalEqualityComparer());
            });
        }

The problem seems to be in last changes of `MongoDbDatabase.CompileAsyncQuery()' method:

public override Func<QueryContext, IAsyncEnumerable<TResult>> CompileAsyncQuery<TResult>(QueryModel queryModel)
            => queryContext => CompileQuery<TResult>(queryModel)(queryContext).ToAsyncEnumerable();

I think the problem in above code is CompileQuery<TResult>(queryModel) executes within lambda and queryModel have been disposed some way on second query execution.

When I revert previous version of this method iisue disappears:

 public override Func<QueryContext, IAsyncEnumerable<TResult>> CompileAsyncQuery<TResult>(QueryModel queryModel)
        {
            var syncQueryExecutor = CompileQuery<TResult>(queryModel);
            return qc => syncQueryExecutor(qc).ToAsyncEnumerable();
        }

related to this comment in another issue: https://github.com/BlueshiftSoftware/EntityFrameworkCore/issues/29#issuecomment-444771018