I am getting this message:
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.
Controller:
[HttpGet]
public IActionResult GetAllTags()
{
try
{
return Ok(GetAll());
}
catch(Exception ex)
{
return ControllerHelper.LogAndReturnBadRequest(_logger, ex);
}
}
private IEnumerable<TagDto> GetAll()
{
IEnumerable<TagDto> tags;
if (!_cache.TryGetValue(CacheKeys.States, out tags))
{
tags = _service.GetAll()?.Select(t => _mapper.Map<TagDto>(t));
if(tags != null)
_cache.Set(CacheKeys.States, tags);
}
return tags;
}
Startup.cs
services.AddMemoryCache();
I was debugging the code line by line, but even after the last line of my code, there is no error at all.
The error I saw was in Kestrel console. Worth to notice, the error only happen when fetching tags from the _cache, not happen on the first time when tags fetched directly from database.
Here is what I got from Postman request:
Many similar questions referring to dispose objects, but here you can see that I do not have dispose(), or using() in my code.