In this example I am going to demo how we can use Business rules engine in BizTalk to route messages dynamically.
Here is our scenario.
We receive a message having a node called PartyName.
Based on the value of PartyName the business rules policy named ‘ResolverRules’ will fetch the endpoint and adaptertype from from a SQL table named ‘RulesResolver’.This policy will be called from an orchestration which will use a dynamic send port based on the endpoint fetched by the rules.
Advantage: This will help us accomplish a decoupled messaging scenario. All we need to do is add the new PartyName and the corresponding endpoints, adapterUsed information in the SQL table and the orchestration dynamic send port will take care of the routing. Which essentially means a single send port can cater to multiple send end points.
STEP1: CREATING THE RULE
Create a DB and table in your SQL server. The table will have three columns for partyname adapter type and end point. Our orchestration will fetch all these information via the rules engine and assign it to the dynamic send port properties. Our table is going to look something like this.
Open the Business Rules Composer.
Go to the facts explorer > Vocabularies tab> Select Vocabularies, right click and pick Add New Vocabularies.
Name your vocabulary as appropriate. Click on your newly created vocabulary and pick Add new definition. We are going to define two definition one will fetch our endpoint and other will fetch the PartyName.
Once done right click on your vocabulary version and pick Publish. Without publishing the vocabulary we wont be able to use that in our rules policy. Also once the vocabulary is published it cannot be modified.If need arises we will have to publish a new version and use that instead.
Now once vocabulary is ready we are going to create rules policy based on the DB facts and the XSD. To reference our xsd we will select the XML schemas tab and right click on schemas > Browse to our xsd file we have created in the BizTalk solution also holding our orchestration.
We are now ready to define our rules policy now. Click on policies and pick add new policy. Name it appropriately. After you are done configuring your policy the end result should look like this. The conditions we have defined has used the vocabularies we defined in the earlier step.
Please go ahead and deploy the policy.Without deploying the orchestration will not pick the rules.
Now our Rules Policy is ready to be used.
STEP 2: CREATING THE ORCCHESTRATION TO CALL RULES.
Please create an orchestration looking ressembling following picture .
You will have to add System.data, System.transaction and Microsoft.RulesEngine reference in the solution. Create three variable in the transaction scope of the call rule shape of the following types.
The expression shape in the orchestration are going to look like this.
Use the call rule shape to point to our rules policy. Configure the input parameters, in this case two one message and other rules connection.
The last send port is dynamic and we will pass the port parameters in the last expression shape which in turn are derived from the message returned by rules policy. Notice the expression to assign the end point.
Now we can deploy this and our solution will be ready for testing.
Please leave me a comment if you have any questions.