In BizTalk Server 2002 I wrote a custom pipeline component in C# to archive inbound messages "just in case" something messed up and the interchange was lost. In BTS2004 I used Gilles Zunino's [MSFT] excellent Archiver pipeline component.
Unfortunately, this component was causing some issues when I began using it in BizTalk Server 2006 due to breaking changes to the .NET Framework, so I needed to update the source code for .NET 2.0 and BTS2006. The new Archive custom pipeline component source code can be downloaded from here!
The "heart" of the code is shown below:
public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
{
Stream originalStrm = null;
FileStream fileArchive = null;
BinaryWriter binWriter = null;
const int bufferSize = 1024;
try
{
IBaseMessagePart bodyPart = inmsg.BodyPart;
if (bodyPart != null)
{
string path = filePath + "." + fileExtension;
path = path.Replace("%MessageID%", inmsg.MessageID.ToString());
originalStrm = bodyPart.GetOriginalDataStream();
fileArchive = new FileStream(path, FileMode.Create, FileAccess.Write);
binWriter = new BinaryWriter(fileArchive);
byte[] buffer = new byte[bufferSize];
int sizeRead = 0;
while ((sizeRead = originalStrm.Read(buffer, 0, bufferSize)) != 0)
{
binWriter.Write(buffer, 0, sizeRead);
}
}
}
catch(Exception ex)
{
System.Diagnostics.EventLog.WriteEntry(ex.Source, ex.Message + "\n" + ex.StackTrace, EventLogEntryType.Error);
}
finally
{
if (binWriter != null)
{
binWriter.Flush();
binWriter.Close();
}
originalStrm.Seek(0, SeekOrigin.Begin);
}
return inmsg;
}
Now why would anyone need to archive inbound messages since BizTalk Server 2006 provides such robust tracking tools?
Two basic reasons:
- I'm very anal about "losing" inbound trading partner data and really hate to ask them to "resend" something, even if it didn't parse in the first place.
- For compliance and audit reasons many customers want to make sure we keep an "unaltered" copy of their original interchange and we almost always normalize documents using a map on the inbound port.
The Archive pipeline component is a really simple way to do satisfy both of these issues and it provides the added value of helping the developer "see" the inbound interchange as it's received in the pipeline.
Please feel free to download and use this code as you see fit. It's provided "as is" however and has not been tested with all the BTS2006 Adapters.
My thanks to Gilles Zunino for his original source code and to Martijn Hoogendoorn for his Pipeline Component Wizard.
Posted
04-08-2006 6:01 PM
by
Jeff Lynch