Available starting with v2.3.0
Composition requests filters allow intercepting composition requests to specific composition handlers. Contrary to endpoint filters that are generic HTTP filters, composition filters sit right in front of the composition handlers they are configured to intercept.
Composition requests filter can be defined as attributes or as classes.
Create an attribute that inherites from CompositionRequestFilterAttribute
like in the following snippet:
public class SampleCompositionFilterAttribute : CompositionRequestFilterAttribute
{
public override ValueTask<object> InvokeAsync(CompositionRequestFilterContext context, CompositionRequestFilterDelegate next)
{
return next(context);
}
}
[!NOTE] The
InvokeAsync
implementation is responsible to invoke the next filter in the pipeline.
Apply the attribute to the composition handlers to intercept:
public class SampleHandler : ICompositionRequestsHandler
{
[SampleCompositionFilter]
[HttpGet("/sample/{id}")]
public Task Handle(HttpRequest request)
{
return Task.CompletedTask;
}
}
Create a class te implements the ICompositionRequestFilter<T>
interface, where the generic T
parameter is the composition handler type to intercept:
public class SampleCompositionFilter : ICompositionRequestFilter<SampleHandler>
{
public ValueTask<object> InvokeAsync(CompositionRequestFilterContext context, CompositionRequestFilterDelegate next)
{
return next(context);
}
}
The above snippet defines a filter intercepting requests to the SampleHandler
composition handler.
[!NOTE] Filters defined as classes implementing the
ICompositionRequestFilter<T>
interface will be automatically registered in DI as transiten, and can use DI to resolve dependencies.