Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Custom Functions in Microsoft Business Rules Engine

The Microsoft Business Rules Engine (BRE), part of Microsoft BizTalk Server, allows for the creation of rich easily readable business rules which can be applied by a BizTalk solution, or any .NET code.   Out of the box, BRE and the main interface Business Rules Composer (BRC) have many different predicates that can be used within a rule, such as Equal, Less Than, Range, Greater Than, Not Equal, Between, and so on.  However, when creating a set of rules for a client, I realized that simple string functions that are not available ‘Out of the Box’  in the BRC.  I decided to create a .NET class which will contain a collection of  string functions for use within business rules.

The first step in creating custom functions that can be called within business rules is to create a .NET assembly project with a static class and all of the public methods for the functions you would like to call.

1 using System; 2  using System.Collections.Generic; 3  using System.Linq; 4  using System.Text; 5 6 namespace Tallan.BizTalk.Generic.Enrichment.Entities 7 { 8 public static class BREFunctions 9 { 10 public static string Concatenate(string string1, string string2) 11 { 12 return string1 + string2; 13 } 14 15 public static string Trim(string string1) 16 { 17 return string1.Trim(); 18 } 19 20 public static string Left(string string1, int positions) 21 { 22 return string1.Substring(0, positions); 23 } 24 } 25 }

Make sure that the assembly is added to the GAC, and open up BRC.  In BRC, create a new policy and within the Fact Explorer browse to your .NET class in the GAC.

image

BREFunctions is the class I have created that contains my custom functions.  The policy that I have created for this is “_myPolicy” and the rule that I have created to demonstrate my custom functions is “”myRule”.

image

Dragging the Trim function from my BREFunctions class to the left hand equal to operand of my IF window adds the function to the rule.  The left hand operand equals ‘”    hasextraspaces    “   my right hand operand equals “hasextraspaces”.  The exists statement is included to facilitate testing my function with an XML document instance.

In testing my rule, I passed in an instance of my test X12EnrichedMessage, and received the following output.

image

As can be seen above, the static class was not executed.  This is because by default, static classes cannot be executed without specifying an instance of our static class.   However, we can bypass this by specifying the “StaticSupport” registry DWORD =1.  To do this, navigate to the following registry entries:

x86: HKLM_LOCAL_MACHINE\\SOFTWARE\Microsoft\BusinessRules\3.0\

x64: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\BusinessRules\3.0

If the StaticSupport DWORD does not exist, create it and set its decimal value to 1.  If it does exist, set the decimal value from 0 to 1.

image

Now retesting my XML instance, we can see that the TRIM custom function was executed successfully.

image

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>