2010-10-14 72 views
2

我有一個BizTalk 2009業務流程,其中包含作爲WCF基本HTTP Web服務發佈的請求 - 響應端口類型。端口有一個操作,並且該操作具有適當模式的請求和響應消息。在此端口上收到請求後,有幾種情況應將錯誤消息返回給客戶端而不是標準響應消息。我很難將正確的錯誤信息返回給客戶端。我希望能夠設置SOAP故障消息的faultcodefaultstring元素。以下是我嘗試過的:BizTalk業務流程:使用非類型化SOAP錯誤進行響應

添加類型爲字符串的故障消息: 我試圖向操作添加帶有消息類型字符串的故障消息。在編排中,我構造了一個字符串消息並將其作爲響應發送。以後被輸送回客戶機故障看起來像:

<?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中實現?

+4

沒有什麼是簡單的BizTalk :) – Nix 2010-10-14 20:00:12

+0

@Nix:同意!我沒有說它*很簡單,只是它*應該*而已。 – 2010-10-14 20:10:00

回答

2

我通過添加一個自定義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> 
+0

雖然我不再能夠測試這種方法,但根據您的結果,這看起來應該是正確的答案。謝謝! – 2013-09-09 13:21:10

0

提醒這個長螺紋我參加了前陣子的: http://social.msdn.microsoft.com/forums/en-US/biztalkr2adapters/thread/f69ec7af-a490-4229-81d4-3d1b41bf9c48/

他們指的是SDK樣本,可以幫助你,但它是一個類型化(不取消類型爲您請求)故障異常。

+0

不幸的是,我從來沒有能夠弄清楚如何應對一個無類型的錯誤,我相當確信這在BizTalk 2009中是不可能的。我最終返回了序列化的字符串,並要求客戶端反序列化消息。 – 2011-11-17 01:05:27

相關問題