Search code examples
filterazureservicebusmessagingazure-servicebus-topics

Custom RuleFilter when filtering a message with Azure Service Bus


Let's suppose I've a topic in Azure Service Bus and this topic receives all the products registered in my application. So, at the end of the day, the body of the message will the serialization of this struct:

public struct Product
{
   public int Id {get; set;}
   public decimal Price {get: set;}
   public DateTime LastOrder {get: set;} 
}

My question is can I implement my own RuleFilter and not rely on the ones provided by the framework such as SqlRuleFilter and CorrelationRuleFilter? I would like to use lambda and do something like that to subscribe for example to the expensive products:

var ruleOptions = new CreateRuleOptions("Default", new ConditionalRule(prod => prod.Price > 1000.00));

I'm trying to create my own ConditionalRule which inherit from the RuleFilter class but I fear that this would not be enough.

Example of the class that I would like to implement:

internal class ConditionalFilter<T> : RuleFilter
{
    private readonly Func<bool, T> _rule;

    public ConditionalFilter(Func<bool, T> rule)
    {
        _rule = rule ?? throw new ArgumentNullException(nameof(rule));
    }

    public override bool Equals(RuleFilter other)
    {
        throw new NotImplementedException();
    }

    public override bool Equals(object obj)
    {
        throw new NotImplementedException();
    }

    internal override RuleFilter Clone()
    {
        return new ConditionalFilter<T>(_rule);
    }
}

Solution

  • can I implement my own RuleFilter and not rely on the ones provided by the framework such as SqlRuleFilter and CorrelationRuleFilter

    No, you cannot. If you want messages to be filtered, you'll have to

    1. Use CorrelationRuleFilter or SqlRuleFilter (most likely the latter)
    2. Promote values from the payload/body to headers