fix: System.ObjectDisposedException for db context into sync secrvice
Some checks failed
.NET Test Pipeline / build-and-test (push) Has been cancelled
Build and Deploy Docker Container / build-and-deploy (push) Successful in 1m45s

This commit is contained in:
2025-02-03 10:55:47 +03:00
parent 885b937b0b
commit b40e394bcf

View File

@ -2,6 +2,7 @@
using Cronos;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@ -191,15 +192,20 @@ public class ScheduleController(ILogger<ScheduleController> logger, IOptionsSnap
filePaths.Add((filePath, defaultCampus[i]));
}
var sync = (ScheduleSynchronizer)ActivatorUtilities.GetServiceOrCreateInstance(provider, typeof(ScheduleSynchronizer));
if (force)
{
dbContext.Lessons.RemoveRange(dbContext.Lessons.ToList());
dbContext.Lessons.RemoveRange(await dbContext.Lessons.ToListAsync());
await dbContext.SaveChangesAsync();
}
_ = sync.StartSync(filePaths, CancellationToken.None);
var scopeFactory = provider.GetRequiredService<IServiceScopeFactory>();
_ = Task.Run(async () =>
{
using var scope = scopeFactory.CreateScope();
var sync = scope.ServiceProvider.GetRequiredService<ScheduleSynchronizer>();
await sync.StartSync(filePaths, CancellationToken.None);
}, CancellationToken.None);
return Ok();
}