Skip to main content

Convert BizTalk MessageBodyPart to String

Get MessageBodyPart as string in Custom Pipeline component execute method.

In the follwoing code pInMsg.BodyPart is converted to String using stream. You can edit this messsage steam as per your requirement then assign to new message traceDataRequest.Message. Following code can be used for tracing the request message recived by BizTalk pipeline or to modifiy the request received.

We have used eventlog entry to log the message for tracing.

                           int bufferSize = 0x280; //  TODO move to external config
                            int thresholdSize = 0x100000;   //  TODO move to external config
                            // Stream inboundStream = bodyPart.GetOriginalDataStream();
                            var virtualStream = new VirtualStream(bufferSize, thresholdSize);
                            var readOnlySeekableStream = new ReadOnlySeekableStream(pInMsg.BodyPart.GetOriginalDataStream(), virtualStream, bufferSize);
                            var memoryStream = new MemoryStream();
                            memoryStream.Flush();
                            IBaseMessagePart bodyPart = pInMsg.BodyPart;

                            virtualStream.CopyTo(memoryStream);
                            Stream Stream1 = bodyPart.GetOriginalDataStream();
                            String test = new StreamReader(Stream1).ReadToEnd();                            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                            Byte[] bytes = encoding.GetBytes(test);
                            Stream1.CopyTo(memoryStream);
                            traceDataRequest.Message = bytes;//memoryStream.ToArray();                            System.Diagnostics.EventLog.WriteEntry("Request message", test);
                            readOnlySeekableStream.Seek(0, SeekOrigin.Begin);


Following are the method which you can directly use in your pipeline component for Getting String data from message and copying the BodyPart to other stream.

Return string message from IBaseMessage

 public string ReadEntireBody(IBaseMessage msg)
        {
            int BufferSize = 0x280; //  TODO move to external config
            int ThresholdSize = 0x100000;   //  TODO move to external config
            string body = null;
            IBaseMessagePart bodyPart = msg.BodyPart;
            Stream inboundStream = bodyPart.GetOriginalDataStream();
            var virtualStream = new VirtualStream(BufferSize, ThresholdSize);
            var readOnlySeekableStream = new ReadOnlySeekableStream(inboundStream, virtualStream,
                                                                    BufferSize);
            var sr = new StreamReader(readOnlySeekableStream);
            body = sr.ReadToEnd();
            return body;
        }


To copy one stream to other Stream

 internal static void CopyDataStream(Stream source, Stream destination)
        {
            byte[] buffer = new byte[2048];
            int length;
            while ((length = source.Read(buffer, 0, 2048)) > 0)
            {
                destination.Write(buffer, 0, length);
            }
            if (!source.CanSeek)
            {
                ReadOnlySeekableStream seekableStream = new ReadOnlySeekableStream(source);
            }
            else
            {
                source.Seek(0, SeekOrigin.Begin);
            }
            destination.Seek(0, SeekOrigin.Begin);
        }

Comments

Popular posts from this blog

WCF MaxReceivedMessageSize max value in .config

MaxReceivedMessageSize parameter in binding config   file:   For max file size in WCF, we need to set following parameter in binding files. <binding name="MyService"        maxReceivedMessageSize="2147483647"        maxBufferSize="2147483647" transferMode="Streamed" >        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"      maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> <!-- other binding info here, such as security --> </binding> Max possible size: MaxReceivedMessageSize to 2147483647 (i.e. Int32.MaxValue), which is 2GB For our case, as we are using WsHttpBinding. <wsHttpBinding>         <binding name="wsHttpBindingSettings" maxReceivedMessageSize="2147483647">      ...

Deploying Custom Pipeline Component on BizTalk Server (PROD)

Deploying Custom Pipeline Component on BizTalk Server: ·         Deploying BizTalk Custom Pipeline Component   on BizTalk Server 2006 or Older Version : To deploy the custom Pipeline component on BizTalk server 2006 or older version. We need to add the Custom Pipeline component in GAC as well as “ C:\Program Files (x86)\Microsoft BizTalk Server 2006\Pipeline Components ” folder.   At design time it will access the Pipeline component dll located in “….. Microsoft   BizTalk Server 2006\Pipeline Components ” folder and show the component in pipeline toolbox. At runtime the BizTalk will use the Custom Pipeline component from GAC. ·         Deploying BizTalk Custom Pipeline Component   on BizTalk Server 2006 R2 or New Version : To deploy the custom Pipeline component on BizTalk server 2006 R2 or later version. We need to add the Custom Pipeline component only in “C:\Pr...

Object Oriented Programming Concepts

OOP Basics Visual Basic was Object-Based, Visual Basic .NET is Object-Oriented , which means that it's a true Object-Oriented Programming Language . Visual Basic .NET supports all the key OOP features like Polymorphism , Inheritance , Abstraction and Encapsulation. It's worth having a brief overview of OOP before starting OOP with VB. Why Object Oriented approach? A major factor in the invention of Object-Oriented approach is to remove some of the flaws encountered with the procedural approach. In OOP, data is treated as a critical element and does not allow it to flow freely. It bounds data closely to the functions that operate on it and protects it from accidental modification from outside functions. OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. A major advantage of OOP is code reusability. Some important features of Object Oriented programming are as follows: Emphasis ...