This is pretty basic stuff, but it is a really common mistake. Using .NET classes in the BTS RuleEngine (RE) is pretty simple until you actually try to run them. The problem always seem to come down to creating an instance of the class that you are referencing in the Vocabulary/ Function etc.
When Testing in the Composer
If you're in the composer things are a little tricky. You will need to create a “Fact Creator” to provide your testing process with an instance of the classes you need. The implementation is simple enough once you get the hang of it, but it is not easily discoverable by trial and error. First I'll give you the main outline, then I'll give you an example of a Fact creator
- Create the helper class with all the helper functions you need (Date comparison operations are a common case here for me)
- Use the Rules Composer to reference the properties/methods of the class that you will need to use in the Rules (I like to add them to the Vocabulary rather than predicates or functions)
- Create a fact retriever (i.e. copy and paste code you find and tweak it to return an instance of the helper class you created in step 1) [Also make sure you have a referece to the Microsoft.RulesEngine Assembly which you can find in the BizTalk installation folder]
- Install the Fact Retriever and the helper class into the GAC
- Click on the policy version, then click test
- Specify the xml instance file from the file system (careful, the engine can modify its contents)
- Specify the fact retriever you just created/installed
- click test and hope for the best
When Calling from Orchestration
Calling from an orchestration is a little different. The fact creator will do nothing to help you here (it is only for composer work). You MUST create an instance of your helper class and pass it down to the rules engine in the Call Rules shape.
Caution: The Call Rules shape is not strict about the “signature“ for the rules engine call. It will allow you to omit the class instance and leave you wondering why your rules never fire.
- Open up the orchestration that is calling the rules
- Create a reference to the assembly that has your helper class
- Create an instance variable for your helper class in the variables of the orchestration
- Select that variable in the Call Rules shape.
Well, that's all for now. Hopefully this will help fill in the blanks that others have left.
___________________________________________________
Below is a sample fact creator
For this example to work you will need to create a reference to the Microsoft.RulesEngine assembly. Look for it in the BizTalk installation directory
using System;
using Microsoft.RuleEngine;
using <Custom Assembly reference here,to the assembly with the helper class>
namespace BTSRulesEngineFactCreator
{
[Serializable]
public class MyFactCreator : IFactCreator
{
private object[] myFacts;
public MyFactCreator()
{
}
public object[] CreateFacts ( RuleSetInfo rulesetInfo )
{
myFacts = new object[1];
//This is where you add instance(s) of your helper class(es) to the myFacts Array
myFacts.SetValue(
new VinProofFacts(),0);
return myFacts;
}
public Type[] GetFactTypes (RuleSetInfo rulesetInfo)
{
return null;
}
}
}