Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access a disposed object. A common cause of this error is disposing a context

I have written a simple application and when I navigate to my edit page the below error pops up.

Microsoft.EntityFrameworkCore.Query[10100]

An exception occurred while iterating over the results of a query for context type 'app.Models.ApplicationDbContext'.

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.

It seems EF is proving a useful information which I can not understand. The tricky part of this error is that it happens randomly when I navigate to the edit page. Sometime it works, Sometimes it fails to load some properties on Edit.cshtml but still works and sometimes the application crashes with provided error just in my console. Another strange happen is that it dose not generate any 500 or 5xx error. It just simply crashes and stop the application.

Here is my Edit.cshtml content:

@page @model EditModel @{     ViewData["Title"] = "Edit Book"; }  <h2>Edit Book</h2>  <div class="row justify-content-center">     <div class="col-md-6">         <form method="post" class="form-border">             <div asp-validation-summary="All" class="validation-container alert alert-danger"></div>             <div class="form-group">                 <label asp-for="Book.Name"></label>                 <input asp-for="Book.Name" class="form-control" />                 <span class="form-text text-danger" asp-validation-for="Book.Name"></span>             </div>             <div class="form-group">                 <label asp-for="Book.Description"></label>                 <input asp-for="Book.Description" class="form-control" />             </div>             <div class="form-group">                 <label asp-for="Book.Author"></label>                 <input asp-for="Book.Author" class="form-control" />             </div>             <input asp-for="Book.Id" type="hidden">             <button type="submit" class="btn btn-primary">Update</button>             <a asp-page="Index" class="btn btn-success">Back To List</a>         </form>     </div> </div> 

Here is My Edit.cshtm.cs OnGet method:

public async void OnGet(int id) {     Book = await _db.Books.SingleOrDefaultAsync(x => x.Id == id);      if(Book == null)     {         RedirectToPage("Index");     } } 

I am using .Net Core 2.2.104

Also when I run command dotnet ef --version it generates Entity Framework Core .NET Command-line Tools 2.2.2-servicing-10034

like image 769
Soheil Avatar asked Apr 05 '19 21:04

Soheil


People also ask

How do I dispose of dbContext in EF core?

When the controller is being disposed, call dispose on your repository and that should dispose the context. If you are using a service layer and not talking to the repository directly from the controller, then call dispose on the service which will call dispose on repo which will dispose the context.

Should let the dependency injection container take care of disposing context instances?

If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. For context, this exception happens because EF Core disposes the connection just after the request to the controller is closed.

What is System ObjectDisposedException?

An ObjectDisposedException is thrown when you try to access a member of an object that implements the IDisposable interface or IAsyncDisposable interface, and that object has been disposed.


1 Answers

This is because of your method return type async void. In general, when you are using async void in your code it’s bad news, because:

  • You can’t wait for its completion
  • Any unhandled exceptions will terminate your process (ouch!)

So return async Task instead of async void from your method as follows:

public async Task OnGet(int id) {     Book = await _db.Books.SingleOrDefaultAsync(x => x.Id == id);      if(Book == null)     {        RedirectToPage("Index");     } } 

For more details:

  • C# – beware of async void in your code

  • Cannot access a disposed object in ASP.NET Core when injecting DbContext

like image 186
TanvirArjel Avatar answered Sep 17 '22 06:09

TanvirArjel