Wednesday 18 February 2015

ReceivePipeline Decode stage MessageType Context Property


I had a situation to capture the MessageType at Receive Pipeline "Decode" Stage  and eventually pass the MessagType to BRE which will executes rule based on MessageType and finally it returns a some Business routing value.

I tried with classic method like below

strMsgType = inmsg.Context.Read("MessageType", BTS_NAMESPACE).ToString();

But during run time I received an error "Object reference not set to an instance of an object."
You may be surprised why would pipeline throw an error to extract MessageType ? but it is true. When I extracted all list of properties at decode stage and couldn't find the MessageType from the Context which is correct because biztalk would generate this property after Disassemble stage.

So how do I get MessageType ? write a custom code ? yes that would be a best option and your hands on.. here is the little code which I have written to build a MessageType at Decode stage. Hope this would help you.
If you have any better Idea, please feel free to drop a comment



 ----------------------------------------------------------------------------------------------------------     
public  string ExtractMessageType(Microsoft.BizTalk.Message.Interop.IBaseMessage message)

        {
            string strValue = String.Empty;
            try
            {
                int bufferSize = 0x280;
                int thresholdSize = 0x100000;

                IBaseMessagePart bodyPart = message.BodyPart;
                Stream inboundStream = bodyPart.GetOriginalDataStream();
                VirtualStream virtualStream = new VirtualStream(bufferSize, thresholdSize);
                ReadOnlySeekableStream originalStrm = new ReadOnlySeekableStream(inboundStream, virtualStream, bufferSize);

                originalStrm.Seek(0, SeekOrigin.Begin);

                XmlTextReader xmlTextReader = new XmlTextReader(originalStrm);
                XPathCollection xPathCollection = new XPathCollection();
                XPathReader xPathReader = new XPathReader(xmlTextReader, xPathCollection);

                while (xPathReader.Read())
                {
                    strValue = xPathReader.NamespaceURI + "#" + xPathReader.LocalName;
                    break;
                }
                originalStrm.Seek(0, SeekOrigin.Begin);
                bodyPart.Data = originalStrm;

                return strValue;
            }
            catch (Exception e)
            {
                return strValue;
            }
        }
----------------------------------------------------------------------------------------------------------