microsoft / RulesEngine

A Json based Rules Engine with extensive Dynamic expression support
https://microsoft.github.io/RulesEngine/
MIT License
3.47k stars 530 forks source link

Common Language Runtime error #511

Open solutions-simplified opened 11 months ago

solutions-simplified commented 11 months ago

I recently upgrade from 4.0.0 to 5.0.1. I am getting errors across the board and it looks like it's related to the breaking change in 5.0.0. In my Utils class I have static methods that take in strongly typed objects (see below). I keep getting a Common Language Runtime error when these rules execute. It appears to be because it can't actually execute my method in the Utils class. Suggestions?

I reviewed the Wiki but the examples shown with Utils is simple string parameters, but do not see any examples with extension methods utilizing strongly typed objects. If I lower back to 4.0.0 everything works as expected.

My expression: customers.Where(CreatedDate >= createdFilterParameter).Where(Utils.IsExistingEmail(Emails, newCustomerDataParameter)).Count() < threshold

public static class Utils
{
    public static bool IsExistingEmail(ICollection<Email> emails, Customer customer)
    {
        if (emails is null || customer is null)
            return false;

    return emails.Any(x => x.Address.Contains(customer.Email));
    }
}   

Thank you in advance for any suggestions - this library is awesome :)

abbasc52 commented 11 months ago

can you provide me details on how you pass inputs to RulesEngine? Also can you try if AutoRegisterInputType = false in ReSettings helps mitigate the issue?

solutions-simplified commented 11 months ago

@abbasc52 here is what I currently have.

var ruleParameters = new RuleParameter[] { new RuleParameter("emails", emailsDataset), new RuleParameter("customer", newCustomer) };

var rulesEngine = new RulesEngine( new Workflow[] { workflow }, new ReSettings { CustomTypes = new Type[] { typeof(Utils) }});

var ruleResults = await rulesEngine.ExecuteAllRulesAsync(workflow.Name, ruleParameters);

solutions-simplified commented 11 months ago

@abbasc52 I wanted to follow up on this - any suggestions :)

abbasc52 commented 11 months ago

@solutions-simplified I assume your customer input is of Type Customer. There is a bug in our dependent lib where if the names are same for input and type(even if case are different), uses the type instead of input.

I am working with the lib to fix this issue, but meanwhile u can use try the below fixes: