GraphQL disposed DataContext

GraphQL disposed DataContext


0

When trying GraphQL in the Playground, I get the following error:

Cannot access a disposed context instance. A common cause of this error is disposing a context instance 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 instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.nObject name: ‘MyDataContext’."

In my .net Project I have a Program.cs file, which registers the Interfaces/DataContext as follows:

builder.Services.AddAdvancedDependencyInjection();
builder.Services.Scan(p => p.FromAssemblies(assemblies)
    .AddClasses()
    .AsMatchingInterface());

builder.Services.AddDbContext<MyDataContext>();

builder.Services.AddGraphQLServer()
    .AddQueryType<MyDataQuery>()
    .AddFiltering()
    .AddSorting()
    .ModifyRequestOptions(opt => opt.IncludeExceptionDetails = true);

In my MyDataQuery.cs I load the data from the IDataService as follows.

class MyDataQuery 
{
    private readonly IDataService _dataService;

    public MyDataQuery(IDataService dataService) 
    {
         _dataService = dataService;
    }

    [UsePaging]
    [HotChocolate.Types.UseFiltering]
    [HotChocolate.Types.UseSorting]
    public async Task<IQueryable<Data>> Data() 
    {
        var data = _dataService.GetData();
        data.ToList(); // Code breaks here, because the dataContext is disposed.

        // some async stuff. 
    }
}

My Service looks like this:

public class DataService : IDataService
{
    public readonly MyDataContext _context;

    public DataService (MyDataContext context)
    {
        _context = context;
    }

    public IEnumerable<Data> GetData()
    {
        return _context.Data;
    }
}

When I use the IDataService at other points, that are entered via a controller, everything works fine.
How should I register the Service/DataContext to make GraphQL work?

4

  • 1

    The HotChocolate warns about the scope of DbContext and shows how to handle this in the EF Core integration docs. In this case though, we can't guess what DataService and IDataService are doing, apart from loading an entire table in memory. That's not how EF Core is meant to be used

    – Panagiotis Kanavos

    1 hour ago


  • There's no AddAdvancedDependencyInjection or Services.Scan in .NET Core. I suspect those methods register DataService as a singleton. AddDbContext registers DbContext as a scoped service, because that makes sense for web apps – a DbContext is a Unit-of-Work, not a database connection or model. In a web app, the scope is the request itself. When a request's processing completes, the DbContext gets disposed. If DataService is a singleton, it will be left with a reference to the disposed DbContext.

    – Panagiotis Kanavos

    1 hour ago

  • Your integration link solved the issue. I missed the point, that services are passed as parameters and not via constructor injection. Thanks!

    – Febertson

    1 hour ago

  • There's another big bug – GetData loads an entire table in memory. At the very least return an IQueryable<> instead of an IEnumerable<>

    – Panagiotis Kanavos

    1 hour ago



Load 5 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *