Backup the Inbound file using the Pipeline and Pipeline component in BizTalk Server 2010

Ever wondered how to back up the HL7 file or for any file for that matter using the pipeline component?

Here is the code implementation of Execute method of pipeline component in decoder stage.

public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            Stream objOriginalStream;
            StreamReader objStreamReader;
            string strFinalMessage;
            IBaseMessagePart bodyPart = pInMsg.BodyPart;

            string fileName = String.Empty;

            try
            {
                if (pInMsg.Context.Read("InboundTransportType", "http://schemas.microsoft.com/BizTalk/2003/system-properties").ToString() == "MLLP")
                    fileName = System.Guid.NewGuid().ToString("d") + ".hl7";
                else
                {
                    fileName = pInMsg.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties").ToString();
                    fileName = Path.GetFileName(fileName);
                   
                }

            }
            catch // Catches any exception and assigns file name to be guid.
            {
                fileName = System.Guid.NewGuid().ToString("d") + ".hl7";  
            }

            if (bodyPart != null)
            {

                objOriginalStream = bodyPart.GetOriginalDataStream();

                if (objOriginalStream != null)
                {
                        objStreamReader = new StreamReader(objOriginalStream);
                        strFinalMessage = objStreamReader.ReadToEnd();

                    //Backup the file unaltered
                        using (StreamWriter sw = new StreamWriter(Path.Combine(backupfilePath, fileName)))
                        {
                            sw.Write(strFinalMessage);
                        }
 

                        //Code to Replace Only CR and LF to {CR}{LF}
                        strFinalMessage = strFinalMessage.Replace("\r\n", "{#EOL#}");        //Replace all types EOL – End of line to one unique string pattern- Start with \r\n.
                        strFinalMessage = strFinalMessage.Replace("\r", "{#EOL#}");
                        strFinalMessage = strFinalMessage.Replace("\n", "{#EOL#}");
                        strFinalMessage = strFinalMessage.Replace("{#EOL#}", "\r\n");

                        int intMSHIndex = strFinalMessage.IndexOf("\r\n");
                        string strMSHSegment = strFinalMessage.Substring(0, intMSHIndex);
                        string strBodySegment = strFinalMessage.Remove(0, intMSHIndex);

                        List<String> lstStr = strMSHSegment.Split(‘|’).ToList<String>();
                        while (lstStr.Count < 16)  //lenght is 1 based index, bounds is 0 based index
                        {
                            lstStr.Add(String.Empty);
                        }
                        lstStr[14] = String.IsNullOrEmpty(strMSH15ConfigurableValue) ? "NE" : strMSH15ConfigurableValue;
                        lstStr[15] = String.IsNullOrEmpty(strMSH16ConfigurableValue) ? "ER" : strMSH16ConfigurableValue;

                        strMSHSegment = String.Join("|", lstStr);
                        strFinalMessage = strMSHSegment + strBodySegment;

               

                    
try
                      { //Try to assign the new stream for downstream components.

                  
                          byte[] byteArray = Encoding.GetEncoding(0).GetBytes(strFinalMessage);
                          bodyPart.Data = new MemoryStream(byteArray);
                    
                      }
                      catch(Exception ex)
                      { //else just pass whatever came in
                          System.Diagnostics.EventLog.WriteEntry("BTIS", "BackupHL7Files – In Catch block – Exception occurred, letting the same data to pass" + ex.ToString());
                          bodyPart.Data.Seek(0, SeekOrigin.Begin);
                          
                      }

                 
   
                }
            }
            return pInMsg;
        }

 

Errors Encountered :

The Pipeline component worked great, when the files were submitted via MLLP from other servers, but it was failing when I received the file from external sources.

Reason: The method or operation is not implemented.

Solution: I implemented the try catch block at the end of Execute method as you can see in the code example.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s