如何在自定义WCF LOB适配器中使用适当的编码检索消息正文?

时间:2011-03-17 22:49:43

标签: biztalk wcf-lob-adapter

我正在尝试编写一个用于BizTalk的基本单向自定义WCF LOB适配器。但是,目标系统不一定支持Xml消息。我理解流经自定义WCF适配器的消息是包装在XML信封中,并且消息正文可以通过以下四种方式之一进行编码:

  • XML
  • 字符串
  • BINHEX
  • 的Base64

此设置受Outbound WCF message body属性的配置控制,该属性接受类似于以下XML片段的属性:

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

在我的Execute类中实现CustomAdapterOutboundHandler方法时,如何检索在发送端口配置中指定了哪种编码?

    /// <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;
    }

1 个答案:

答案 0 :(得分:1)

终于找到了..

以下代码就可以了。

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
}