Search code examples
biztalkwcf-lob-adapter

How to Retrieve the Message Body with an Appropriate Encoding in a Custom WCF LOB Adapter?


I'm trying to write a basic One-Way custom WCF LOB Adapter for use in BizTalk. However, the target system does not necessarily supports Xml messages. I understand that messages flowing through a custom WCF adapter are wrapped in an XML envelope and that the message body can be encoding in one of four ways :

  • Xml
  • String
  • BinHex
  • Base64

This settings is governed by the configuration of the Outbound WCF message body property, which accepts an property that looks like the following XML fragment :

<bts-msg-body xmlns='http://www.microsoft.com/schemas/bts2007' encoding='[xml|base64|hex|string]'/>

In the implementation of the Executemethod in my CustomAdapterOutboundHandler class, how is it possible to retrieve which encoding has been specified on the send port configuration ?

    /// <summary>
    /// Executes the request message on the target system and returns a response message.
    /// If there isn’t a response, this method should return null
    /// </summary>
    public Message Execute(Message message, TimeSpan timeout)
    {
        // ISSUE: how to retrieve the message body as binary byte[] / stream / whatever ?
        // <bts-msg-body xmlns='http://www.microsoft.com/schemas/bts2007' encoding='[xml|base64|hex|string]'/>

        System.Xml.XmlDictionaryReader reader = message.GetReaderAtBodyContents();

        return null;
    }

Solution

  • Finally found out..

    Code like the following will do.

    object strvalue;
    bool result = Message.Properties.TryGetValue("http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties#OutboundXmlTemplate", out strvalue);
    if (result)
    {
      XmlDocument xmldoc = new XmlDocument();
      xmldoc.LoadXml(strvalue.ToString());
      string btsencoding = xmldoc.SelectSingleNode("//*[local-name()='bts-msg-body']").Attributes["encoding"].Value;
    
    // do something useful
    }