Context Disposed into a service

.net-core entity-framework-core

Question

Got following error, when i try to use my context into a custom service :

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

My code is composed of a controller and a service. The (simplified) code of my controller is :

public class IcsController : Controller
{
    public string current_Directory = System.IO.Directory.GetCurrentDirectory();
    //Importation du context (BDD)
    private readonly Sync4All_AngularContext _context;
    private readonly UserManager<User> _userManager;
    public readonly OverbookingsManager _overbookingsManager;

    private IIcsService _icsService;

    public IcsController(Sync4All_AngularContext context, UserManager<User> userManager, IIcsService icsService, OverbookingsManager overbookingsManager)
    {
        _context = context;
        _userManager = userManager;
        _icsService = icsService;
        _overbookingsManager = overbookingsManager;
    }

    //---------------------------------Telechargement et Update de tous les ICALS-----------------------------------
    [HttpGet]
    public ActionResult DownloadAndUpdate()
    {
        _overbookingsManager.SendEmailsOverbookings();

        return Ok();
    }
}

The service is defined as followed in configuration services

services.AddScoped<OverbookingsManager>();

And the code of the service is as below :

    public class OverbookingsManager
{
    private readonly Sync4All_AngularContext _context;
    public OverbookingsManager(Sync4All_AngularContext context)
    {
        _context = context;
    }

    public async void SendEmailsOverbookings()
    {
        List<Overbookings> overbookinsList =  await _context.Overbookings.Where(m => DateTime.Compare(m.DtEmailSent, DateTime.Today.AddDays(-7)) < 0).ToListAsync(); //This is where i got the error of context disposed
        //blabla rest of my code 


    }
}

When i do a get request on my controller, it calls the methode DownloadAndUpdate(), which use the service.

Others methodes of my controller and services use _context, but i never do _context.dispose().

I don't understand the problem, could you please help ?

Thanks

1
1
2/17/2019 9:50:43 AM

Accepted Answer

The problem is void:

public async void SendEmailsOverbookings()

Replace that with Task:

public async Task SendEmailsOverbookings()

For some explanation take a look at the documentation:

For methods other than event handlers that don't return a value, you should return a Task instead, because an async method that returns void can't be awaited.

0
2/17/2019 12:14:15 PM


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