Skip to content

Using the DbContext through constructor injection in the service layer results in errors #5257

Closed

Description

Is your feature request related to a problem?

So in our project we want to design our application in a way that we can easily add different API interfaces like GraphQL or a RestAPI on our existing business logic.

To achieve this we moved as much logic as possible to regular Services which live in the DI container. This services are made using regular microsoft standards, like injecting the DbContext normally through the constructor.

Now we do have a small problem, for Queries we want the service to pass back a IQueryable of the DbSet in the DbContext and pass this to the Query class so we can use the projection option and only retrieve the fields that the query requests.

Problem is that when injecting the DbContext in the service the regular way it will start throwing an Cannot access a disposed context instance error.

I have an example project here: https://github.com/bramvelderen-nlo/Playday.GraphQL

The solution you'd like

What i hope is possible is that i can just build my service layer the same way as i would for an MVC project and reuse the exact logic for the GraphQL resolvers. But right now that gives me the DbContext error. So is there already an existing solution here where i don't need to pass the DbContext from the Query class to the service and instead can just rely on the injected DbContext?

Product

Hot Chocolate

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions