Ashiqul
Posted on August 13, 2021
Developers might forget to invoke ActionExecutionDelegate
from ASP.NET Core filter IAsyncActionFilter.OnActionExecutionAsync
. Which is essential for the HTTP request to go through the whole pipeline. So forgetting to invoke ActionExecutionDelegate
will essentially terminate the request processing through the pipeline.
For ease I use following abstract
base class which just wraps the interface and ennsures ActionExecutionDelegate
is being invoked.
public abstract class BaseAsyncActionFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
await OnActionExecutionAsync(context);
await next();
}
protected abstract Task OnActionExecutionAsync(ActionExecutingContext context);
}
Following xunit test verifies that ActionExecutionDelegate
will be invoked.
[Fact]
public async Task OnActionExecutionAsync_Should_Invoke_Action_Execution_Delegate()
{
// Arrange
// setup fixture (using nuget package AutoFixture)
var fixture = new Fixture();
fixture.Register(() => new ParameterDescriptor());
fixture.Customize(new AutoMoqCustomization());
var hasActionExecutionContextInvoked = false;
Task<ActionExecutedContext> Next()
{
hasActionExecutionContextInvoked = true;
return Task.FromResult(fixture.Create<ActionExecutedContext>());
}
var sut = new SpyBaseAsyncActionFilter();
// Act
await sut
.OnActionExecutionAsync(fixture.Create<ActionExecutingContext>(), Next);
// Assert
hasActionExecutionContextInvoked.ShouldBeTrue();
}
public class SpyBaseAsyncActionFilter : BaseAsyncActionFilter
{
protected override Task OnActionExecutionAsync(ActionExecutingContext context)
{
return Task.CompletedTask;
}
}
💖 💪 🙅 🚩
Ashiqul
Posted on August 13, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
csharp Why Span<T> and Ref Structs Can't Directly Participate in Asynchronous Operations
November 26, 2024