2013-05-13 5 views
7

제 3 자 비누 서비스를 성공적으로 사용하고 있습니다. 자동으로 클래스를 생성 한 비누 웹 서비스에 서비스 참조를 추가했습니다. WCF FaultException 응답에서 세부 정보 추출

오류가이 같은 비누 응답을 반환 발생

: 나는 오류를 잡을 수 있지만 세부 사항을 추출 할 수 없습니다
<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Body> 
     <SOAP-ENV:Fault> 
     <faultcode>SOAP-ENV:Client</faultcode> 
     <faultstring xsi:type="xsd:string">Error while reading parameters of method 'Demo'</faultstring> 
     <detail xsi:type="xsd:string">Invalid login or password. Connection denied.</detail> 
     </SOAP-ENV:Fault> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

. 나는 다음과 같은 코드를 시도 :

(가) 세부 노드로 다음과 그것을 오류가 객체가 아닌 그러나
catch (FaultException ex) 
{ 
    MessageFault msgFault = ex.CreateMessageFault(); 
    var elm = msgFault.GetDetail<string>(); 
    //throw Detail 
} 

:

Expecting element 'string' from namespace 'http://schemas.datacontract.org/2004/07/MyDemoNamespace'.. Encountered 'Text' with name '', namespace ''. 

이 타사 API 그래서 난 응답을 변경할 수 있습니다.

답변

10

메시지 결함의 세부 노드에 XML이 포함될 것으로 예상됩니다. GetDetail은이 XML을 주어진 객체로 deserialize합니다.

내용이 XML이 아니기 때문에이 방법을 사용할 수있었습니다.

당신은 그러나 XML에 접근하고 innerXml 값을 읽을 수 있습니다

MessageFault msgFault = ex.CreateMessageFault(); 
var msg = msgFault.GetReaderAtDetailContents().Value; 

이 일에 접근했다. 응답이 꺾쇠 괄호가 포함 된 경우

+1

아직도 – John

+0

내 문제를 완벽하게 해결 [] 문제가 있습니다! – eouw0o83hf

+0

detail이 문자열이 아닌 경우이 코드는 ok입니다. MessageFault msgFault = ex.CreateMessageFault(); XmlReader readerAtDetailContents = msgFault.GetReaderAtDetailContents(); var readOuterXml = readerAtDetailContents.ReadOuterXml(); var data = XElement.Parse (readOuterXml); 사전 <문자열, 문자열> 요소 data.Elements =() .ToDictionary (elementKey => elementKey.Name.LocalName, elementVal => elementVal.Value, NULL); – Jerome2606

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

     if (reply.IsFault) 
     { 
      // Create a copy of the original reply to allow default WCF processing 
      MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue); 
      Message copy = buffer.CreateMessage(); // Create a copy to work with 
      reply = buffer.CreateMessage();   // Restore the original message 

      MessageFault faultex = MessageFault.CreateFault(copy, Int32.MaxValue); //Get Fault from Message 
      FaultCode codigo = faultex.Code; 
      //if (faultex.HasDetail)... //More details 

      buffer.Close(); 
+3

해설을 추가 할 수 있습니까? – Robert