ASP.Net Entity Framework Core Error: Cannot access a disposed object

jon
 
on May 15, 2019 03:30 AM
19580 Views

Hi,

in my asp.net core 2.1 app im trying to implement background job with SignalR and Coravel

The job read data from database and write to another

If i execute the code without Coravel i get no error but in opposite case

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: 'CompanyDbContext' 

        public IActionResult StartProgress(ICollection<string> tables)
        {
            string jobId = Guid.NewGuid().ToString("N");
            _queue.QueueAsyncTask(() => SyncTablesJobAsync(jobId, tables));

            return RedirectToAction("Progress", new { jobId });
        }

        private async Task SyncTablesJobAsync(string jobId, ICollection<string> tables)
        {
            var taskList = new List<Task>();
            try
            {
                foreach (var table in tables)
                {
                    switch (table)
                    {
                        case "agenti":
                            var agenti = FillAgenti();
                            taskList.Add(agenti);
                            break;
                       ...
                    }
                    await _hubContext.Clients.Group(jobId).SendAsync("progress", numerTablesProcessed / tables.Count);
                    await Task.WhenAll(taskList);
                }
            }
          
        }

So if i run same method with Task<IActionResutlt> without Coravel all works

the link i take inspiration

https://www.jerriepelser.com/blog/communicate-status-background-job-signalr/

Download FREE API for Word, Excel and PDF in ASP.Net: Download
jon
 
on May 16, 2019 07:37 AM
on May 16, 2019 07:37 AM

I solved so

this is solution: have to use IServiceProvider to give DbContext scope to Coravel

                using (var scope = _scopeFactory.CreateScope())
                {
                    var appDbCtx = scope.ServiceProvider.GetService<ApplicationDbContext>();
                    var companyDbCtx = scope.ServiceProvider.GetService<CompanyDbContext>();

                    foreach (var table in tables)
                    {                 
                        switch (table)
                        {
                            case "agenti":
                                processed += await FillAgenti(appDbCtx, companyDbCtx);
                                break;
                            case "rubrica":
                                processed += await FillRubrica(appDbCtx, companyDbCtx);
                                break;
                          ...
                        }
                       
                        
                        await _hubContext.Clients.Group(jobId).SendAsync("progress", (processed / tables.Count) * 100);                
                    }