Getting System.ObjectDisposedException when redirecting pages

asp.net-core c# entity-framework-core razor-pages

Question

Update: The problem was that the OnGet methods was async. Thank you everyone for the help and input!

In my first page (Home) i'm getting a list of Users:

HomePageModel:

public async void OnGetAsync()
    {
            var everything = await _context.Users.ToListAsync();
            GetUsers = everything;
    }

Then i'm creating a list of the users on the page and a link that redirects to a page that has another list of the users bets:

The HomePage:

<ul class="list-group">
@foreach (var user in Model.GetUsers)
{
<li class="list-group-item">
    //Irrelevant stuff
    <a asp-page="./UserBets" asp-route-id="@user.Id">View Bets</a>
</li>
}
</ul>

That redirects to UserBets, here's the UserBetsModel:

public async void OnGetAsync(string Id)
    {
        if (Id != null)
        {   //Debugging             
            Message += Id;
            using (var db = _context)
            {
                var everything = await _db.Bet.Where(a => a.UserId == Id).Select(s => s.MatchId).SingleAsync();
                GetBets = everything;
            }
        }            
    }

When it gets there i'm getting a message that says:

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. Object name: 'AsyncDisposer'.'

I've tried withouth having using scopes, having it on both pagemodels. The thing is that i don't really understand the problem here, so i'm sorry if i'm not giving you enough context or code but ask if you need more.

What i want to do is print out a list of the users bets. Thanks in advance!

1
0
9/6/2018 8:37:26 AM

Popular Answer

using (var db = _context)
{
    var everything = await _context.Bet.Where(a => a.UserId == Id).Select(s => s.MatchId).SingleAsync();
    GetBets = everything;
}

Your using will dispose of _context immediately after the GetBets = everything; line. The problem is that _context doesn't belong to your method, so any other code that uses _context will find it disposed. I imagine you have similar code elsewhere that is disposing of it too, which is probably why the issue is presenting here.

Don't dispose of shared objects until they aren't going to be used anymore (usually when the containing class is destroyed - you should add your own IDisposable implementation on the parent class).

2
9/6/2018 7:59:04 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow