I was using CacheResponseAttribute on one of the Get methods in the service like [CacheResponse(Duration = 60)]. But I want this cache duration to come from a config file so I can set it to be different depending on the environment the service is currently running on (dev, prod, etc)
I know we can't use something that's not constant as a parameter in the attribute constructor. So I was planning to do something like
public class MyCacheResponseAttribute : CacheResponseAttribute
{
public IConfiguration Configuration { get; set; }
public CacheWidgetResponseAttribute()
{
int.TryParse(Configuration["cache_duration_in_secs"], out var cacheDuration);
Duration = cacheDuration;
}
}
and use this as the decorator on the Get method. However, the dependency injection doesn't seem to work for the attributes since I'm getting the Configuration as null.
My return type is string, I've tried ToOptimizedResultUsingCache but I couldn't get it to return string properly.
What options do I have? Is it possible to make the IoC work on Attributes somehow? I guess as a last resort I could have a ICacheClient in the service and use it but that would be my last resort since it's gonna be more custom made.