An instance of the Biztalk MessageContext is created and stays with each message until the message leaves Biztalk. It contains properties such as RecievePortName, RecieveFileName, MessageType i.e. all the meta data for a message.


If you want to modify this meta data you need to modify the IBaseMessageContext using either the following methods, this may be done in either a custom pipeline component or inside an orchestration:


IBaseMessageContext.Promote: Make the Message Context property a promoted property


IBaseMessageContext.Write: Make the Message Context Property a distinguished property


Where you make the call to modify the message context depends on WHEN it should be modified. If you want to use a message context property to correlate two messages you should do it inside a custom pipeline component. If you want to set the message context during processing after some decision logic do so in a Message Assignment shape in an Orchestration. We are only looking at doing it in a custom pipeline component.


An example of using this would be if you had two input files which needed to be correlated in an orchestration based on an id or date in the filename which defined a relationship between those files. Another example of a problem this would solve is found here: http://www.eggheadcafe.com/ng/microsoft.public.biztalk.server/post21008841.asp


So in both these cases we want to look at the file name of the message, extract and promote part of it so we can either use that extracted information to either correlate two or more incoming messages or base some decision logic on those properties. The way I’m going to show you how to do it is to actually overwrite the RecieveFileName property with the extracted value from it.


Notes:


              I am going to show you how we did it instead of just giving you code or a solution.


              Although I think “Biztalk Server 2004 Unleashed” tries too cover too much in one book it does have a very good explanation of Pipelines and what the different stages of a pipeline should implement.  


1)      Download Martijn Hoogendoorns’ Pipeline Component Wizard http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=1d4f7d6b-7d27-4f05-a8ee-48cfcd5abf4a and install it:


a.       Unzip the source code, open the Visual Studio sln file and rebuild the PipelineComponentWizardSetup project.


b.       Right-click the PipelineComponentWizardSetup project and select “Install” to install it.


2)      In your Biztalk project add a new project and select Biztalk Server Pipeline Component Project set the following properties in the wizard.






3)     Explaining all of the Interfaces which are implemented in a pipeline component is not in the scope of this post. I will   however advise you to take a quick look and familiarise your self with the following methods which implement the PropertyBag: Load, Save, ReadPropertyBag, WritePropertyBag.


The method of the Decode Pipeline Component which does the Biz is the Execute method to which we add the following code:


public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)


{


//Make sure the message contexts property name isn’t empty


       if (_PropertyName != null)


                     {


              //Get the current value of the property


              object oPropertyValue = inmsg.Context.Read(_PropertyName, _PropertyNamespace);


              if (oPropertyValue != null)


              {


                     string sPropertyValue = (string)oPropertyValue;


                     System.Text.RegularExpressions.Regex oReplaceRegularExpression =


                     new


                     System.Text.RegularExpressions.Regex(_ReplaceRegularExpression);


                                 


                     //Replace the regular expression with the value specified


              sPropertyValue =


              oReplaceRegularExpression.Replace(sPropertyValue,_Value);


                                 


                     //Either promote or distinguish the property


                     if (_PromoteOrDistinguish == “Promote”)


                     {


                           inmsg.Context.Promote(_PropertyName, _PropertyNamespace,


                           sPropertyValue);


                     }


                     else if (_PromoteOrDistinguish == “Distinguish”)


                     {


                            inmsg.Context.Write(_PropertyName, _PropertyNamespace,


                           sPropertyValue);


                     }


              }


                                                        


return inmsg;


}


Note I have also changed the Value property to allow you replace with an empty string so you can remove characters using a regular expression.


        public string Value


        {


            get


            {


                return _Value;


            }


            set


            {


              if (value == null)


              {


                     _Value = string.Empty;


              }


              else


              {


                     _Value = value;


              }


               


            }


        }


4)     Well that’s the development easy aye thanks to the Pipeline Component Wizard. As for testing I guess you could do this by STOP PRESS a much better way of debugging a pipeline:


              referencing the new pipeline component project in your Biztalk project


              change the output path in the pipeline components project properties dialog to C:\Program Files\Microsoft BizTalk Server 2004\Pipeline Components


              adding the compiled pipeline dll as a component by right clicking on the pipeline component toolbar and choosing add new item then selecting the Biztalk Pipeline Components Tab


              placing a breakpoint in the Execute method code


              add the component to the decode stage of a pipeline where you want to use it set the properties on the decode component to (this is just a suggestion, the regular expression tries to remove all the characters around a date which was in a filename setting the ReceivedFileName property to a date)



              deploy your Biztalk project


              set up a receive port & location


              attach to the BTSNTSvc,exe process


              drop a file in the receive location and hopefully the debug runtime will stop at your breakpoint


5)     To use the modified and promoted RecieveFileName property to correlate two messages


              create a Correlation Type and set the “Correlation Type Properties” property to FILE.ReceivedFileName


              create a Correlation Set based on this type


              initialise the correlation set property of the receive shape (for the messages which need to be correlated) to the name of the correlation set you created above


R. Addis & Emil @ Microsoft