【问题标题】:BizTalk Orchestration: Respond with Untyped SOAP FaultBizTalk 编排:响应无类型 SOAP 错误
【发布时间】:2011-04-25 14:53:15
【问题描述】:

我有一个带有请求-响应端口类型的 BizTalk 2009 业务流程,它作为 WCF Basic-HTTP Web 服务发布。该端口有一个操作,该操作具有带有适当模式的请求和响应消息。在此端口上接收到请求后,在少数情况下应该向客户端返回故障消息而不是标准响应消息。我很难将正确的故障消息返回给客户端。我希望能够同时设置 SOAP 错误消息的 faultcodefaultstring 元素。这是我尝试过的:

添加字符串类型的故障信息: 我尝试将消息类型为字符串的故障消息添加到操作中。在编排中,我构建了一个字符串消息并将其作为响应发送。交付回客户端的故障如下所示:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
         <faultstring xml:lang="en-US">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;string>This is the error message.&lt;/string></faultstring>
         <detail>
            <ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
               <HelpLink i:nil="true"/>
               <InnerException i:nil="true"/>
               <Message>&lt;?xml version="1.0" encoding="utf-8"?>
&lt;string>This is the error message.&lt;/string></Message>
               <StackTrace>at Microsoft.BizTalk.Adapter.Wcf.Runtime.BizTalkAsyncResult.End() ...</StackTrace>
               <Type>Microsoft.BizTalk.Adapter.Wcf.Runtime.BizTalkNackException</Type>
            </ExceptionDetail>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

这几乎可以工作,除了 faultstring 元素包含我的字符串的 xml 序列化版本而不是字符串本身。我也无法设置faultcode 元素。

添加http://schemas.xmlsoap.org/soap/envelope/#Fault类型的故障消息 我想如果我构造Fault 元素并发送它,我想我可能能够说服 BizTalk 按照我所期望的那样返回错误消息。所以我添加了一个类型为http://schemas.xmlsoap.org/soap/envelope/#Fault 的故障消息,构建了适当的消息并将其作为响应发送。结果与上面相同,除了 faultstring 元素包含一个 CDATA 部分,其中包含我在其中构建的整个 xml 消息,而不是字符串。

所以我现在卡住了;我觉得这应该是 BizTalk 中的一项简单任务。 MSDN 上的文档How to Throw Fault Exceptions from Orchestrations Published as WCF Services 没有告诉您“如何”抛出错误异常,除了可以抛出它们并且您需要在配置中设置includeExceptionDetailInFaults(我已经这样做了)。

有人对如何在 BizTalk 2009 中实现这一点有任何建议吗?

【问题讨论】:

  • 使用 BizTalk 没有什么是简单的 :)
  • @Nix:同意!我并没有说它简单,只是它应该是。

标签: wcf soap biztalk biztalk-2009


【解决方案1】:

我通过添加自定义 WCF IDispatchMessageInspector 解决了这个确切的问题,我从序列化消息中读取原因文本,然后使用反序列化的原因文本返回新的 System.ServiceModel.FaultException 消息。

在 BizTalk 业务流程中,我使用 System.String 作为 PortType 错误消息类型。

public class HandleUntypedSoapFault : IDispatchMessageInspector
{

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {

        if (reply.IsFault)
        {

            MessageBuffer buffer = reply.CreateBufferedCopy(int.MaxValue);
            MessageFault messageFault = MessageFault.CreateFault(buffer.CreateMessage(), int.MaxValue);

            if (!messageFault.HasDetail)
            {
                reply = buffer.CreateMessage();
                return;
            }

            using (XmlReader reader = XmlReader.Create(new StringReader(messageFault.Reason.ToString())))
            {
               reader.MoveToContent();
               string _faultText =  reader.ReadElementContentAsString();
            }

            reply = Message.CreateMessage(
                reply.Version, 
                new FaultException(
                    _faultText, 
                    new FaultCode("client")).CreateMessageFault(),
                null);
        }
    }
}

现在 SoapFault 看起来更像这样:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode>s:Client</faultcode>
         <faultstring xml:lang="en-US">An untyped SoapFault from my BizTalk orchestration. This text was set in the orchestration.</faultstring>
      </s:Fault>
   </s:Body>
</s:Envelope>

【讨论】:

  • 虽然我不再能够测试这种方法,但根据您的结果,这看起来应该是正确的答案。谢谢!
【解决方案2】:

想起了我不久前参与的一个长线程: http://social.msdn.microsoft.com/forums/en-US/biztalkr2adapters/thread/f69ec7af-a490-4229-81d4-3d1b41bf9c48/

它们引用了一个可能对您有所帮助的 SDK 示例,但它是一个类型化(不是您要求的非类型化)错误异常。

【讨论】:

  • 遗憾的是,我一直无法弄清楚如何对未键入的错误做出响应,并且我相当确信这在 BizTalk 2009 中是不可能的。我最终返回了序列化的字符串并要求客户反序列化消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
相关资源
最近更新 更多