How to consume PHP SOAP service using WCF

Posted by mr.b on Stack Overflow See other posts from Stack Overflow or by mr.b
Published on 2010-06-08T16:26:47Z Indexed on 2010/06/11 4:03 UTC
Read the original article Hit count: 795

Filed under:
|
|
|
|

I am new in web services so apologize me if I am making some cardinal mistake here, hehe.

I have built SOAP service using PHP. Service is SOAP 1.2 compatible, and I have WSDL available. I have enabled sessions, so that I can track login status, etc.

I don't need some super security here (ie message-level security), all I need is transport security (HTTPS), since this service will be used infrequently, and performances are not so much of an issue.

I am having difficulties making it to work at all. C# throws some unclear exception ("Server returned an invalid SOAP Fault. Please see InnerException for more details.", which in turn says "Unbound prefix used in qualified name 'rpc:ProcedureNotPresent'."), but consuming service using PHP SOAP client behaves as expected (including session and all).

So far, I have following code. note: due to amount of real code, I am posting minimal code configuration

PHP SOAP server (using Zend Soap Server library), including class(es) exposed via service:

<?php

class Verification_LiteralDocumentProxy {

    protected $instance;

    public function __call($methodName, $args)
    {
        if ($this->instance === null)
        {
            $this->instance = new Verification();
        }

        $result = call_user_func_array(array($this->instance, $methodName), $args[0]);
        return array($methodName.'Result' => $result);
    }
}

class Verification {

    private $guid = '';
    private $hwid = '';

    /**
    * Initialize connection
    *
    * @param string GUID
    * @param string HWID
    * @return bool
    */
    public function Initialize($guid, $hwid)
    {
        $this->guid = $guid;
        $this->hwid = $hwid;
        return true;
    }

    /**
    * Closes session
    *
    * @return void
    */
    public function Close()
    {
        // if session is working, $this->hwid and $this->guid
        // should contain non-empty values
    }
}

// start up session stuff
$sess = Session::instance();

require_once 'Zend/Soap/Server.php';
$server = new Zend_Soap_Server('https://www.somesite.com/api?wsdl');

$server->setClass('Verification_LiteralDocumentProxy');

$server->setPersistence(SOAP_PERSISTENCE_SESSION);

$server->handle();

WSDL:

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="https://www.somesite.com/api" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="Verification" targetNamespace="https://www.somesite.com/api">
    <types>
        <xsd:schema targetNamespace="https://www.somesite.com/api">
            <xsd:element name="Initialize">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="guid" type="xsd:string"/>
                        <xsd:element name="hwid" type="xsd:string"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="InitializeResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="InitializeResult" type="xsd:boolean"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="Close">
                <xsd:complexType/>
            </xsd:element>
        </xsd:schema>
    </types>
    <portType name="VerificationPort">
        <operation name="Initialize">
            <documentation>
                Initializes connection with server</documentation>
            <input message="tns:InitializeIn"/>
            <output message="tns:InitializeOut"/>
        </operation>
        <operation name="Close">
            <documentation>
                Closes session between client and server</documentation>
            <input message="tns:CloseIn"/>
        </operation>
    </portType>
    <binding name="VerificationBinding" type="tns:VerificationPort">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="Initialize">
            <soap:operation soapAction="https://www.somesite.com/api#Initialize"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
        <operation name="Close">
            <soap:operation soapAction="https://www.somesite.com/api#Close"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="VerificationService">
        <port name="VerificationPort" binding="tns:VerificationBinding">
            <soap:address location="https://www.somesite.com/api"/>
        </port>
    </service>
    <message name="InitializeIn">
        <part name="parameters" element="tns:Initialize"/>
    </message>
    <message name="InitializeOut">
        <part name="parameters" element="tns:InitializeResponse"/>
    </message>
    <message name="CloseIn">
        <part name="parameters" element="tns:Close"/>
    </message>
</definitions>

And finally, WCF C# consumer code:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IVerification
{
    [OperationContract(Action = "Initialize", IsInitiating = true)]
    bool Initialize(string guid, string hwid);

    [OperationContract(Action = "Close", IsInitiating = false, IsTerminating = true)]
    void Close();
}

class Program
{
    static void Main(string[] args)
    {
        WSHttpBinding whb = new WSHttpBinding(SecurityMode.Transport, true);

        ChannelFactory<IVerification> cf = new ChannelFactory<IVerification>(
            whb, "https://www.somesite.com/api");

        IVerification client = cf.CreateChannel();

        Console.WriteLine(client.Initialize("123451515", "15498518").ToString());
        client.Close();
    }
}

Any ideas? What am I doing wrong here?

Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about php