CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeff Lynch [MVP]

Everything E-Commerce!

BizTalk Server 2006: Archive Pipeline Component

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.


Published Apr 08 2006, 06:01 PM by jlynch
Filed under:

Comments

jlynch said:

Nick,

This component was design for receive pipelines as a "Decoder" component only. It should work as an "Encoder" component but you'll need to add the encoder component category attribute to the class. Also, there is a difference between using the GetOriginalDataStream() method and the BodyPart.Data property that is not well documented. The GetOriginalDataStream method returns a cloned stream which is seekable, so that's what I use.

# July 17, 2007 1:33 PM

Anand said:

i'm not able to call enterprise library 2.0 exception policy inside pipeline custom component even thoug i added appdomain to configure app.config.

# August 4, 2007 5:36 AM

SidAhmed said:

I'm working in the enterprise Mattel in Mauritania. We are interested for the applications, which does the DMS(Document management System) . We finf the BIzTalk Server, but we don'tknow if BizTalk Server 2006 archive.
# October 4, 2007 9:20 AM

michael isbell said:

the file for the archive pipeline component is corrupted and can't be downloaded.

misbell_contractor@ap.org

# December 13, 2007 1:40 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

This Blog

Syndication