I try to inject my DbContext using services.AddDbContext in my Startup.cs. It's gets injected in the service that needs to handle every call to the DB. This AzureService gets injected in other services that actually need the data. When I try this however I always get a system.ObjectDiposedException on de DbContext inside AzureService.
When I inject it directly in the CompareService it just work, so I'm probably making a grave mistake with the nested services. Help/insights much appreciated.
code:
Startup.cs
services.AddDbContext<SchoolDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SchoolSyncDb")));
services.AddScoped<IAzureService, AzureService>();
services.AddScoped<ICompareService, CompareService>();
AzureService
public class AzureService : IAzureService
{
private SchoolDbContext schoolDbContext;
private IMapper mapper;
private IHelperService helperSrv;
public AzureService(SchoolDbContext _sdb, IMapper _mapper, IHelperService _helper)
{
schoolDbContext = _sdb;
mapper = _mapper;
helperSrv = _helper;
}
CompareService
public class CompareService: ICompareService
{
private readonly IWisaService wisaSrv;
private readonly ISmartschoolService smtSrv;
private readonly IAzureService azureSrv;
private readonly IHelperService helperSrv;
private readonly ILoggerService loggerSrv;
public CompareService(IWisaService _ws, ILoggerService _ls, ISmartschoolService _ss, IAzureService _as, IHelperService _hs)
{
wisaSrv = _ws;
smtSrv = _ss;
azureSrv = _as;
helperSrv = _hs;
loggerSrv = _ls;
}
trying to get data from the AzureService in the CompareService fails because the DbContext in the AzureService is Diposed.
List<AdbStudentModels> DbList;
try
{
DbList = azureSrv.GetAllStudentInDbBySchool(school);
}
In AzureService
public List<AdbStudentModels> GetAllStudentInDbBySchool(string school)
{
try
{
return schoolDbContext.AdbStudentModels.Where(i => i.School.Schoolnaam.Equals(school)).ToList();
}
catch (Exception ex)
{
throw ex;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…