microsoft / microsoft/RulesEngine

Subject: Query Regarding RulesEngine: Execution error - "Unable to cast object of type 'System.Linq.Expressions.InstanceMethodCallExpression1' t.o type 'System.Linq.Expressions.ParameterExpression"

Open
#499 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs investigation
Dominant language
C#
Stars
4.4k
Forks
616
Avg merge
6m
Merged PRs (30d)
2

Description

Hello,

I am writing to seek assistance regarding an issue we are encountering while using the RulesEngine. We have a RulesExpression with multiple rules, where the output of one rule is used as input for another rule. However, we are experiencing an error during the execution of the second rule's expression.

Here are the details of our RulesExpression:

{
  "RuleExpression": [
    {
      "WorkflowName": "DataBuilder",
      "Rules": [
        {
          "RuleName": "GetAuthorDetails",
          "Expression": "input1.ApiOutputs.FirstOrDefault(x => x.ExternalApiIdentifier == \"NessieSearchPeopleApi\") != null",
          "Actions": {
            "OnSuccess": {
              "Name": "OutputExpression",
              "Context": {
                "Expression": "input1.ApiOutputs.FirstOrDefault(x => x.ExternalApiIdentifier == \"NessieSearchPeopleApi\").Input.people"
              }
            }
          }
        }
      ]
    },
    {
      "WorkflowName": "DataBuilder",
      "Rules": [
        {
          "RuleName": "GetAuthorDetails",
          "Expression": "input2 != null",
          "Actions": {
            "OnSuccess": {
              "Name": "OutputExpression",
              "Context": {
                "Expression": "input2.FirstOrDefault(x => x.fullName != null);"
              }
            }
          }
        }
      ]
    }
  ]
}

We have a C# code snippet that parses and executes the rules:

var newInput = new Object();
var workflows = JsonConvert.DeserializeObject<List<Workflow>>(payload.RuleExpression.ToString());
foreach (var workflow in workflows)
{
    var workflowList = new List<Workflow>();
    workflowList.Add(workflow);
    var bre = new RulesEngine.RulesEngine(workflowList.ToArray(), null);
    List<RuleResultTree> resultList = bre.ExecuteAllRulesAsync("DataBuilder", resolverInput, newInput).Result;
    var resultFromRule = resultList[0].ActionResult.Output;
    newInput = resultFromRule;       
}

During the execution of the second rule's expression, we are encountering the following error: 'Exception while executing OutputExpressionAction: Unable to cast object of type 'System.Linq.Expressions.InstanceMethodCallExpression1' to type 'System.Linq.Expressions.ParameterExpression'.'

The error occurs specifically in the expression: "Expression": "input2.FirstOrDefault(x => x.fullName != null);"

We would greatly appreciate any insights, suggestions, or guidance on how to resolve this error and successfully execute the second rule's expression.

Thank you in advance for your assistance.
Best regards, Amina

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure using the supplied workflows and C# snippet, starting at ExecuteAllRulesAsync and the OutputExpressionAction error. Compare the first workflow's output with the second expression using input2.FirstOrDefault; done means the second rule executes without the InstanceMethodCallExpression1 cast error and returns the intended result.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.