Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

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

Description

@IvoryRubble

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:
#29 (comment)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions