Asp.Net Core EF Core Error: 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.


Error: Cannot access a disposed of object. A common cause of this error is disposing of 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 of context instances.

 

Solution: Below are things, you can try to fix the problem:

  1. Make your DB Context Service a Transient in the Startup.cs class
  2. Change the service lifetime from services.AddTransient to services.AddScoped
  3. If you are using async void in your service function replace it with async Task
  4. Another common scenario for this exception is when you are trying to get access to the Database Context in an Event Handler. The Event Handler might lose State resulting in the Database Context to be null.

    - One way to solve this is by creating another function in the Controller Class then accessing the Database Context from that function. 

    - If you want to log the outcome of an event that is handled in the EventHandler function, use SignalR to stream the message from the Event then post the message back to the Server-side using Ajax, that way the message will be logged using the Database Context.
  5. If you are working with Event Handler and would like to get access to the Context, try to move the Event Handler from the Controller and create a Repository Class that would consume an Interface. Then register the Repository Class (containing the Event handler enheriting the Interface) as a Transient in Startup.cs.

    - When the Event Handler has been called in the Repository Class, streem or broadcast the event with SignalR or implement Publisher and Subcriber relationship to the Event Handler so that your function in another class can get notified once the Event has been emitted.

    Problem: If the Event Handler resides in the same Controller, the Database Context is disposed immidietly when the execution enters the await mode inside the Event Handler. This causes the Application lose state of the Injected Dipendencies therefore the Database Context is disposed as well.

    Leave a comment and let me know how you resolved this issue.




Linda said:

Hey folks, listen up! DO NOT CALL _context.SaveChanges without an Wait in front of it, if you do you will see all sorts of different behaviour resulting into this error. Make sure that when you are using an Async Function directive, always chain your function call using async and await. Like Jack mentioned, this is a Rabbit hole.

Posted On: March 08, 2022 14:56:43 PM
Jack said:

This is by far the most annoying error in EF Core, when you make your function an Async and start using the Thread Tasks you are in it for a Rabbit Hole. You will chain all your function calls using Async Await. What a mess!!

Posted On: March 08, 2022 14:54:13 PM


For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]