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);
}
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
Post a Comment