.Net 5 动作过滤器在使用 AspNetCore.CacheOutput 时不起作用
.Net 5 Action filters not working when using AspNetCore.CacheOutput
我有一个 .Net 5 Web Api poc,这是我从 .Net Framework 迁移到 .Net Core 的一部分。作为此迁移的一部分,我正在使用中间件 AspNetCore.CacheOutput 以便我们可以结合使用 client/server 缓存。
在代码方面
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
.
.
.
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
}
CacheOutputOverrideAttribute.cs - 动作过滤器 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);
}
}
}
使用Postman,发帖如下,收到正确回复。
http://localhost:30186/api/Response/GetPlaylist/HU7MOHRMHBOEP6HG4GXK64ZOC4
无论我做什么,OnAction 过滤器都不会执行。我很困惑为什么会这样。我找不到任何丢失的代码。这是 AspNetCore.OutputCache 的问题吗?
当代码为 运行 时,输出 window 仅记录以下内容。如果模型绑定无效,为什么我会收到正确的结果?此外,虽然我收到了正确的结果,但数据也没有被缓存。
OnResultExecuting
OnResultExecuted
我的同事发现 OnActionExecuting 和 OnActionExecuted 事件不起作用的原因是 ASPNetCore.CacheOutput 中间件混淆了它们。为了有一个类似的事件,现在必须使用 OnActionExecutionAsync 事件。
我有一个 .Net 5 Web Api poc,这是我从 .Net Framework 迁移到 .Net Core 的一部分。作为此迁移的一部分,我正在使用中间件 AspNetCore.CacheOutput 以便我们可以结合使用 client/server 缓存。
在代码方面
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
.
.
.
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
}
CacheOutputOverrideAttribute.cs - 动作过滤器 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);
}
}
}
使用Postman,发帖如下,收到正确回复。
http://localhost:30186/api/Response/GetPlaylist/HU7MOHRMHBOEP6HG4GXK64ZOC4
无论我做什么,OnAction 过滤器都不会执行。我很困惑为什么会这样。我找不到任何丢失的代码。这是 AspNetCore.OutputCache 的问题吗?
当代码为 运行 时,输出 window 仅记录以下内容。如果模型绑定无效,为什么我会收到正确的结果?此外,虽然我收到了正确的结果,但数据也没有被缓存。
OnResultExecuting
OnResultExecuted
我的同事发现 OnActionExecuting 和 OnActionExecuted 事件不起作用的原因是 ASPNetCore.CacheOutput 中间件混淆了它们。为了有一个类似的事件,现在必须使用 OnActionExecutionAsync 事件。