I have written a code like follows utilizing ToOptimizedResultUsingCacheAsync method, but I am getting 405 status code. And getting a error saying
Serializing Task’s is not supported. Did you forget to await it?
I have added the await on my method as well.
public async Task<object> GetAsync(RequestDTO request)
{
return await Request.ToOptimizedResultUsingCacheAsync(CacheAsync,Defaults.CacheKey, TimeSpan.FromSeconds(60),
async () =>
{
var result = await GetResultAsync();
// Processing the result adding some filteration and pagination to it.
return new ResponseDTO
{
Response = resultToReturn,
// Extra details regarding the result and the filteration or anything which has been applied.
}
}
}
Ahh thanks for the clarification about the methods.
I will use the method you suggested.
One more thing regarding the cache attribute.
If I use [CacheResponse(Duration = 60)] attribute on my method it will cache its response.
This is working fine but what if I have to invalidate the response which is cached by the above attribute how will I do that?
As we are not specifying any key in the attribute nor we have any overload that supports specifying the key.
var cacheObj = await CacheAsync.ResolveFromCacheAsync(Defaults.ResponseCacheKey, Request);
if (cacheObj != null)
return cacheObj;
// Rest of the method to get from DB
// Setting the cache
await CacheAsync.CacheAsync(Defaults.ResponseCacheKey, responseToReturn, Request, TimeSpan.FromMinutes(Defaults.CacheExpiryInMinutes));
I did use this method as you suggested and its working properly for me, contents are getting cached I am getting 200 and 304 as and when required.
But I am unable to write the tests for it using NUnit Framework.
I am unable to Mock the IRequest and it is getting passed as null so all of the tests are failing.
var cache = appHost.Container.Resolve<ICacheClientAsync>();
var mockRequest = new Mock<IRequest>();
var mockServices = new Mock<Services>();
mockServices.Setup(s => s.CacheAsync.CacheAsync(Defaults.ResponseCacheKey, new Response { }, mockRequest.Object, TimeSpan.FromMinutes(Defaults.CacheExpiryInMinutes), CancellationToken.None).AsTaskResult());
var Services = mockSystemServices.Object;
// Explicitly specify all arguments, avoid relying on optional parameters
var response = await Services.GetAsync(new List());
Assert.IsInstanceOf<Response>(response);
var Response = response as Response;
Assert.NotNull(Response);
Assert.AreEqual(3, Response.Items.Count);
Assert.AreEqual("A", Response.Items[0].Name);
Assert.AreEqual("B", Response.Items[1].Name);
Assert.AreEqual("C", Response.Items[2].Name);
I am doing this but the IRequest is getting passed as null always.
I am having the OneTimeSetUp() as well in which I am intializing the DB in memory and resolving the dependancy.