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

Last Reply 6 months ago By jon

Posted 7 months ago

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/

This question does not have replies that have been liked.