BizTalk Message Helper Methods
Posted: Tuesday, April 17, 2007 12:21 AM
by
matt
Filed under: Newsletter, BizTalk
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