ciao
perdonate ma ho provato da solo a capire ma nulla
Quando lancio un metodo che legge le tabelle da un database ottengo un errore che dice:
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'.
uso la DI di asp.net core per passare il contesto e non faccio io il dispose
startup:
services.AddDbContextPool<ApplicationDbContext>((serviceProvider, options) =>
{
options.UseSqlite(Configuration.GetConnectionString("appDbConnection"));
});
services.AddDbContextPool<CompanyDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("companyDbConnection")));
Controller e metodo
public SyncTablesController(CompanyDbContext companyContext,
ApplicationDbContext applicationDbContext, IMapper mapper, IQueue queue, IHubContext<JobProgressHub> hubContext)
{
this._companyContext = companyContext;
this._applicationContext = applicationDbContext;
this._mapper = mapper;
...
}
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);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
private async Task FillAgenti()
{
var agents = await new Agent(_companyContext).GetAgentsAsync();
var agenteRepository = new AgenteRepository(_applicationContext, _mapper);
await agenteRepository.RemoveAllAgentiAsync();
await agenteRepository.AddAgentiAsync(
await agenteRepository.FillAgentiFromCompanyAsync(agents));
numerTablesProcessed++;
}
Anche nei repository uso la DI e passo al costruttore CompanyDbContext
ho fatto il debug tante volte ma non capisco dove chiudo il contesto
Se l'errore è evidente fatemi sapere per favore