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.
My guess is you need to hydrate the results of your query before storing in in the cache. Linq uses deferred execution, which means that the underlying source won't actually be queried until you try to enumerate the results. So you are just storing a query in the cache, and by the time you try to retrieve the actual data, the underlying context is disposed.
Add a ToList
to your query and store the list in the cache:
tags = _service.GetAll()?.Select(t => _mapper.Map<TagDto>(t)).ToList();
I would also note that the result of a linq query that returns a sequence will never be null. It might be empty, so if you don't want to cache an empty result you could change your null check to if (tags.Any())
.