Is there a way to run NRule Engine in asyncronously?

565 views Asked by At

I want to run all rules asyncronously to make it thread safe

When i m performing load test then why RuleEngine taking so much time to execute all rules.

        NRuleRepository repository = null;
        foreach (var rule in rules)
        {                
            repository = new NRuleRepository();
            repository.LoadRules(rule.Rule);
            var factory = repository.Compile();
            var session = factory.CreateSession();
            NRuleBody data = null;
            foreach (var fact in rule.RuleDataList)
            {
                data = new NRuleBody();
                data.Rule = rule.Rule;
                data.RuleData = fact;
                session.Insert(data);
            }

            result += session.Fire();
        }

Can i make call as below:

session.FireAsync();

or there is any other option to fire Multiple rules but in async ? and NRuleRepository class should be reinitialize on every request ?

1

There are 1 answers

3
Nicholas Tyrrell On

At the very least, you could probably utilise Task.Run() in order to create a thread for each instance of the repository, but what you're doing seems very inefficient.

Why are you inserting the rule with the data in the session? You've already added the rule to the repository.

If you are only ever having a singular rule in the repository, NRules is almost certainly overkill, and you would be better placed doing almost anything else.