Home > Back-end >  .Net 5 Action filters not working when using AspNetCore.CacheOutput
.Net 5 Action filters not working when using AspNetCore.CacheOutput

Time:11-08

I have a .Net 5 Web Api poc that I am doing as part of a migration from .Net Framework to .Net Core. As part of this migration, I am using the middleware AspNetCore.CacheOutput so that we may do a combination of client/server caching.

In terms of the code

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddControllers();
        .            
        .
        .
        services.Configure<ApiBehaviorOptions>(options =>
        {
            options.SuppressModelStateInvalidFilter = true;
        });
    }

CacheOutputOverrideAttribute.cs - Action Filter class

namespace ResponseCaching.Cache
{
    public class CacheOutputOverrideAttribute : CacheOutputAttribute
    {
        public override void OnResultExecuted(ResultExecutedContext context)
        {
            Debug.WriteLine("OnResultExecuted");
            base.OnResultExecuted(context);
        }

        public override void OnResultExecuting(ResultExecutingContext context)
        {
            Debug.WriteLine("OnResultExecuting");
            base.OnResultExecuting(context);
        }

        public override void OnActionExecuted(ActionExecutedContext context)
        {
            Debug.WriteLine("OnActionExecuted");
            base.OnActionExecuted(context);
        }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            Debug.WriteLine("OnActionExecuting");
            base.OnActionExecuting(context);
        }
    }
}

** ResponseController.cs **

namespace ResponseCaching.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ResponseController : ControllerBase
    {
        [HttpGet]
        [Route("GetPlaylist/{TestId}")]
        [CacheOutputOverride(ClientTimeSpan = 300, ServerTimeSpan = 1200)]
        public IActionResult GetPlaylist(string testId)
        {
            var result = this.responseService.GetCacheOverrideData(testId);
            return Ok(result);
        }
    }
}

Using Postman, the following is posted and the correct response is received.

http://localhost:30186/api/Response/GetPlaylist/HU7MOHRMHBOEP6HG4GXK64ZOC4

No matter what I do, the OnAction filters do not get executed. I am puzzled as to why this is happening. I cannot find any missing code. Is this an issue with AspNetCore.OutputCache?

When the code is run, the Output window only logs the following. If the model binding is invalid, why am I receiving the correct result? Also, while I am receiving the correct result the data is also not being cached.

OnResultExecuting
OnResultExecuted

CodePudding user response:

My coworker found that the reason the OnActionExecuting and OnActionExecuted events were not working is that the ASPNetCore.CacheOutput middleware obfuscates them. In order to have a similar event, one must now use the OnActionExecutionAsync event.

  • Related