Skip to content

Filter

Subscriber filters are similar to ASP.NET MVC filters and are mainly used to process additional work before and after the subscriber method is executed. Such as transaction management or logging, etc.

Create subscribe filter

  1. Create filter

Create a new filter class and inherit the SubscribeFilter abstract class.

public class MyCapFilter: SubscribeFilter
{
    public override void OnSubscribeExecuting(ExecutingContext context)
    {
        // before subscribe method exectuing
    }

    public override void OnSubscribeExecuted(ExecutedContext context)
    {
        // after subscribe method executed
    }

    public override void OnSubscribeException(ExceptionContext context)
    {
        // subscribe method execution exception
    }
}

In some scenarios, if you want to terminate the subscriber method execution, you can throw an exception in OnSubscribeExecuting, and choose to ignore the exception in OnSubscribeException.

To ignore exceptions, you can setting context.ExceptionHandled = true in ExceptionContext

  1. Configuration

Use AddSubscribeFilter<> to add a filter.

services.AddCap(opt =>
{
    // ***
}.AddSubscribeFilter<MyCapFilter>();

Currently, we do not support adding multiple filters.