BizTalk Message Helper Methods

Posted: Tuesday, April 17, 2007 12:21 AM by matt
Filed under: ,

This entry describes two helper methods to interact with messages in a BizTalk orchestration.

The first helper methods will return the string representation of a BizTalk message. The methods are as below:

/// <summary>
/// Pass in a BizTalk Message will return a string
/// </summary>
/// <param name="btsMessage">The BizTalk message to get the string from</param>
/// <returns>The string from the Body of the BTS Message</returns>
public static string GetStringFromBTSMessageBody(XLANGMessage btsMessage)
{
  string result;
  StreamReader reader = new StreamReader((Stream)btsMessage[0].RetrieveAs(typeof(Stream)));
  result = reader.ReadToEnd();
  reader.Close();
  return result;
}

Or:

/// <summary>
/// Pass in a BizTalk Message Part and will return a string
/// </summary>
/// <param name="btsMessage">The BizTalk message part to get the string from</param>
/// <returns>The string from BTS Part</returns>
 public static string GetStringFromBTSMessagePart(XLANGPart btsPart)
 {
   string result;
   StreamReader reader = new StreamReader((Stream)btsPart.RetrieveAs(typeof(Stream)));
   result = reader.ReadToEnd();
   reader.Close();
   return result;
 }

Therefore in an orchestration expression shape, the following code will return the string representation of a BizTalk message using the helper method:

// Below will return the string from a BizTalk Orchestration message called msgBTS.
// strFromBTSMsg is a string variable declared in the orchestration
strFromBTSMsg = BTSClassHelper.MessageHelper.GetStringFromBTSMsgPart(msgBTS);

An alternative to using the above helper method is to create a System.Xml.XmlDocument variable in the orchestration,
then assign the BizTalk message to the variable. Then as below, the OuterXml can be extracted from the XmlDocument:

varDom = msgBTS;
strFromBTSMsg = varDom.OuterXML;

The downside to using the XmlDocument variable, is that the whole message will be loaded into memory and an extra XmlDocument variable must be created in the orchestration.

The second helper method (CreateBTSMsgFromString) will construct a BizTalk message from a string. This method is a copy from this post, with a few minor modifications.
The referenced post describes how a binary message in an orchestration can be constructed programmatically.
Just as a side note, remember a BizTalk message can be:
a) xml
b) Anything else
ie (Word Document, PDF, excel spreadsheet, jpg, flat file,  etc. etc. etc.)

The helper classes can be downloaded at the end of this blog entry, so I will not repeat the code for the helper method CreateBTSMsgFromString.

Therefore in an orchestration expression shape, the following code will construct a BizTalk message from a string using the helper method:

// strFromBTSMsg is a string variable declared in the orchestration
// msgBTS is the BizTalk message to be constructed
// the last parameter is the encoding to apply to the message
BTSClassHelper.MessageHelper.CreateBTSMsgFromString(strFromBTSMsg,
                                                    msgBTS,
                                                    BTSClassHelper.MessageFactoryEncoding.UTF8);  
 

An alternative to using the above helper method is to create a System.Xml.XmlDocument variable in the orchestration,
Then as below, load in the string and then assign a BizTalk message to the XmlDocument

strFromBTSMsg = "<Message><FirstName>Bob</FirstName></Message>"
varDom.LoadXml(strFromBTSMsg);
msgBTS = varDom;

The downside to the above approach compared to the helper method is the extra overhead of the System.Xml.XmlDocument variable.

You can download the helper classes and a quick example on how to use them Here (Zip File)
Read this before trying to run

 

Comments

Yossi Dahan

April 17, 2007 4:03 AM

Simple and to the point. great post - thanks.

Chris

April 20, 2007 7:42 AM

Matt, you said the XMLDocument drawback is that it loads the whole doc in memory, but when using the helper class and returning the document in a string, don't you also load the whole doc in memory? I'm confused...

matt

April 20, 2007 8:20 AM

Hi Chris,

For the memory requirements, you may be correct. But if I need the string from a BizTalk message, I am skipping the overhead of assigning the BizTalk message to the dom and then extracting the string from the dom. The helper method will return the string without any use of a dom variable. Also when contructing a BizTalk message, again, I am skipping the whole dom variable in the orchestration. I use these methods all the time. They are nice, because I can skip the xmldocument variable declaration and assignment in the orchestration.

Chris

April 20, 2007 9:12 AM

Thanks Matt for the clarification... that helped.

Sandeep

September 6, 2007 10:26 AM

Thanks Matt. It was quite helpful.

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS