Introduction

In Biztalk, the developer has been provided with a very strong tool called as Business Rules Engine, which  can be used to evaluate certain conditions and then output some values based on the evaluation result. It basically is used to decouple the if then else logic inside the orchestration or a pipeline component or even a helper class for that matter.  This use of BRE to evaluate the conditions and producing output saves a lot of plumbing code inside the orchestration or custom components. The only thing that needs to be done is to create a business rules policy and deploy it in the Business Rules Engine Database.  Based on forum question, the OP had a requirement to map certain values to the destination based upon the certain incoming values and the number of If-Else condition that would have arisen out of this would have blown out of the proportions as newer and newer values would have been introduced. The question can be found here . In such case an increase in the number of the conditions of evaluation would result in strong coupling between the process and the map as every change in the condition would have to be taken care of in the mapper. In such case, creating a BRE policy and executing it in the mapper is the best way to any change in the input condition needs to be handled only in the BRE policy and not in the mapper this thus helps in turn to avoid deployment of Biztalk apps and all that needs to be deployed is a new version of Policy. This article aims at providing a basic understanding of the concept of calling BRE policy from a mapper.

Dummy Business Requirement

The front end will provide a xml of the Employees and one of the fields would be a designation Id. Based Upon the designation Id the Destination system should get the designation of the employee.
For Example,
Request from front end: 
<ns0:Employeees xmlns:ns0="http://brefrommappoc.inputmessage/">
  <Employee>
    <EmployeeName>Mandar</EmployeeName>
    <EmployeeId>12345</EmployeeId>
    <EmployeeDesignationCode>3</EmployeeDesignationCode>
  </Employee>
  <Employee>
    <EmployeeName>Dharmadhikari</EmployeeName>
    <EmployeeId>007</EmployeeId>
    <EmployeeDesignationCode>2</EmployeeDesignationCode>
  </Employee>
  <Employee>
    <EmployeeName>EmployeeName_0</EmployeeName>
    <EmployeeId>EmployeeId_0</EmployeeId>
    <EmployeeDesignationCode>1</EmployeeDesignationCode>
  </Employee>
</ns0:Employeees>

Response To destination:

<ns0:Employees xmlns:ns0="http://brefrommappoc.outputmessage/">
  <Employee>
    <EmployeeName>Mandar</EmployeeName>
    <EmployeeId>12345</EmployeeId>
    <EmployeeDesignation>Senior Developer</EmployeeDesignation>
  </Employee>
  <Employee>
    <EmployeeName>Dharmadhikari</EmployeeName>
    <EmployeeId>007</EmployeeId>
    <EmployeeDesignation>Junior Developer</EmployeeDesignation>
  </Employee>
  <Employee>
    <EmployeeName>EmployeeName_0</EmployeeName>
    <EmployeeId>EmployeeId_0</EmployeeId>
    <EmployeeDesignation>Trainee</EmployeeDesignation>
  </Employee>
</ns0:Employees>

Design

The design can be broadly divided into three categories

  1. Create a Biztalk Project
  2. Create a BRE Policy
  3. Create a helper class to call the BRE
  4. Create a Map in the Biztalk project and call the Helper class

Create a BizTalk Project

Follow the steps given below to create the Biztalk Project.

  1. Create a Biztalk project as shown below.
  2. Input message has schema structure as shown below
  <xs:element name="Employeees">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="Employee">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="EmployeeName" type="xs:string" />
              <xs:element name="EmployeeId" type="xs:string" />
              <xs:element name="EmployeeDesignationCode" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

     4.    Output message has schema structure as shown below

  <xs:element name="Employees">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="Employee">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="EmployeeName" type="xs:string" />
              <xs:element name="EmployeeId" type="xs:string" />
              <xs:element name="EmployeeDesignation" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

 5.    BREMessage has schema structure as shown below and will be used to call the BRE policy and storing the output of the Policy Execution.

  <xs:element name="BREMessage">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="DesignationId" type="xs:string" />
        <xs:element name="Designation" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
    6.     The map will be added later on.

Creating a BRE Policy

Follow the below steps.

  1. Open the Business Rules Composer wizard
  2. Select the BREMessage created above as an xmlschema in the facts explorer, refer below screen shot. Navigate to the Biztalk Project and then select the BREMessage.xsd(This approach is for POC and Concept purpose, ideally the devlopment should be done using the .net classes facts)
  3. Now create three different rules as shown in a sample below.
  4. Create rules 2, 3 in a similar fashion by assigning some dummy values to the Designation.
  5. Save the Policy and Publish and Deploy it.

Creating Helper class to call the BRE

Create a class library project as shown below.

The code inside the Helper class should be like shown below. First, the reference to Microsoft.RuleEngine.dll should be added.
using Microsoft.RuleEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace BRECallHelperClass
{
    public class HelperClass
    {
          
          
        public string CallBRE(string strDesignationId)
        {
  
  
            string strDesignation;
            try
            {
                System.Diagnostics.EventLog.WriteEntry("Designation Id", strDesignationId);
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.LoadXml("<ns0:BREMessage xmlns:ns0='http://brefrommappoc.bremessage/'><DesignationId>" + strDesignationId + "</DesignationId><Designation>Designation_0</Designation></ns0:BREMessage>");
                TypedXmlDocument txd = new TypedXmlDocument("BREMessage", xDoc);
                Policy pol = new Policy("EmployeeDesignation");
                pol.Execute(txd);
                System.Diagnostics.EventLog.WriteEntry("POlicy Execution output", txd.Document.OuterXml);
  
                strDesignation = txd.Document.SelectSingleNode("/*[local-name()='BREMessage' and namespace-uri()='http://brefrommappoc.bremessage/']/*[local-name()='Designation' and namespace-uri()='']").InnerText;
                System.Diagnostics.EventLog.WriteEntry("Designation as derived from rule", strDesignation);
  
                  
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Exception Thrown is ", ex.Message);
                return string.Empty;
            }
            return strDesignation;
        }
  
    }
}
Strong Name the project using snk and build the project. Add the reference of this project to the Biztalk Project created in step 1. Also, Install the assembly in the GAC.

Calling the BRE from MAP

Add the map IPToOp.btm to the BizTalk Project created in step1 and select the input as Inputmessage.xsd and output as OutputMessage.xsd
Configure the Scripting functoid as shown below

Strong Name the Biztalk Project, Provide the Biztalk Application Name and Deploy the Solution.

Testing

Once the Application is deployed, create a receive port and a send port. Apply the map as an inbound map on the send port and set a filter on the receive port name on the send port so that the send port will subscribe to the messages published by the receive port, For testing purposes, the receive and send ports can use the FILE protocol. If the input message is put as the one provided in the dummy requirement, the output will be as mentioned in the dummy requirements.

See Also

Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki