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

后端 未结 4 2386
梦谈多话
梦谈多话 2020-12-16 13:32

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

Microsoft.EntityFrameworkCore.Query[10100]

<
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 13:57

    What I am about to post is NOT the answer to this particular question. But it is related so just to save somebody headache I am posting it. I was encountering this same error

    System.ObjectDisposedException: Cannot access a disposed object. etc

    The following was the code with the bug (can you see it?):

    [HttpGet("processs/oxxo-spei/ticket-email/{paymentIdx}")]
    public StatusCodeResult ProcessOxxoSpeiTicketEmailAsync(string paymentIdx)
    {
        var paymentId = paymentIdx.DecodeRef();
                
        var response = _orderEngine.ProcessOxxoSpeiTicketEmailAsync(paymentId);
    
        return StatusCode(200);
    }

    The following change fixed it:

    [HttpGet("processs/oxxo-spei/ticket-email/{paymentIdx}")]
    public async Task ProcessOxxoSpeiTicketEmailAsync(string paymentIdx)
    {
        var paymentId = paymentIdx.DecodeRef();
                
        var response = await _orderEngine.ProcessOxxoSpeiTicketEmailAsync(paymentId);
                    // ^^^^I HAD FORGOTTEN TO PUT AWAIT
        return StatusCode(200);
    }

    Yes that's right I had forgotten to put "await" before a function that used an EF Core dbcontext. Adding 'await' fixed it. So easy to miss it, especially if you're tired and under deadline.

提交回复
热议问题