Menu

Fix passing SOAP faults from NuSoap to C#

Help
2011-03-25
2013-11-08
  • Pawel Gradziel

    Pawel Gradziel - 2011-03-25

    Hi all,

    There is weird bug in .NET that requires from SOAP Fault data to have explicit order of properties.
    Problem occurs when you get nusoap fault response from PHP read your .NET application. You get strange exception:

    Server returned an invalid SOAP Fault. Please see InnerException for more details

    Inner exception reveals more details:

    Element ‘faultstring’ with namespace name ” was not found. Line 6, position 126

    This problem is quite well described here:
    http://www.dirty-motherfucker.org/blog/2010/04/27/passing-soap-faults-from-nusoap-to-c/

    Solution for this quite easy: you need to reorder properties in nusoap_fault::serialize() method with this order:
    1. faultcode
    2. faultstring
    3. faultactor
    4. faultdetail

    So the new serialize() method for nusoap_fault class should looks like this:

    function serialize() {
          $ns_string = '';
          foreach( $this->namespaces as $k => $v ) {
            $ns_string .= "\n  xmlns:$k=\"$v\"";
          }
          $return_msg =
            '<?xml version="1.0" encoding="' . $this->soap_defencoding . '"?>' .
            '<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"' . $ns_string . ">\n" .
              '<SOAP-ENV:Body>' .
                '<SOAP-ENV:Fault>' .
                  $this->serialize_val( $this->faultcode,   'faultcode'   ) .
                  $this->serialize_val( $this->faultstring, 'faultstring' ) .
                  $this->serialize_val( $this->faultactor,  'faultactor'  ) .
                  $this->serialize_val( $this->faultdetail, 'detail'      ) .
                '</SOAP-ENV:Fault>' .
              '</SOAP-ENV:Body>' .
            '</SOAP-ENV:Envelope>';
          return $return_msg;
    }
    

    Could anyone please put this into HEAD revision? It will not affect existing code, just make .NET clients working with nusoap.

    Best regards,
    Paweł Grądziel

     
  • elmer fudd

    elmer fudd - 2011-09-12

    Paweł is completely right:

    According to soap envelope schema (http://schemas.xmlsoap.org/soap/envelope/) Fault is defined as sequence:

    <xs:complexType name="Fault" final="extension" >
        <xs:annotation>
      <xs:documentation>
        Fault reporting structure
      </xs:documentation>
    </xs:annotation>
        <xs:sequence>
          <xs:element name="faultcode" type="xs:QName" />
          <xs:element name="faultstring" type="xs:string" />
          <xs:element name="faultactor" type="xs:anyURI" minOccurs="0" />
          <xs:element name="detail" type="tns:detail" minOccurs="0" />     
        </xs:sequence>
      </xs:complexType>

    It means all Fault elements has to be placed in xml in correct order:
    - faultcode
    -  faultstring
    - faultactor
    - detail

    Xml generated with nusoap is incorrect according to schema.

     
  • ShaX

    ShaX - 2013-11-08

    After pulling me hair for few hours I came up to this.
    Thanks Pawel, would be nice if someone actually puts this in HEAD.
    Also, using var $soap_defencoding = 'ISO-8859-1' as default breaks .NET clients when nusoap_fault is returned, IMHO there is no reason not to use utf-8 as default.

     

    Last edit: ShaX 2013-11-08

Log in to post a comment.