如何使用WCF使用PHP SOAP服务

时间:2010-06-08 16:26:47

标签: c# php wcf soap soap-rpc-encoded

我是网络服务的新手,如果我在这里犯了一些重大错误,请向我道歉,呵呵。

我使用PHP构建了SOAP服务。服务与SOAP 1.2兼容,我有WSDL可用。我已启用会话,以便我可以跟踪登录状态等。

我这里不需要一些超级安全性(即消息级安全性),我需要的只是传输安全性(HTTPS),因为这种服务很少使用,性能也不是问题。

我很难让它完全奏效。 C#引发了一些不明确的异常(“服务器返回了无效的SOAP错误。请参阅InnerException以获取更多详细信息。”,反过来说“在限定名称中使用未绑定前缀'rpc:ProcedureNotPresent'。”),但使用PHP SOAP客户端消耗服务行为符合预期(包括会话和所有)。

到目前为止,我有以下代码。 注意:由于实际代码的数量,我发布了最少的代码配置

PHP SOAP服务器(使用Zend Soap Server库),包括通过服务公开的类:

<?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>

最后,WCF C#消费者代码:

[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();
    }
}

有什么想法吗?我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

您是否尝试过从wsdl生成客户端代理?具有数据协定的本地副本(XML架构)或托管的wsdl。

您应该能够创建一个简单的C#控制台应用程序,在wsdl上执行“添加服务引用...”并创建代理。客户端代码将自动生成,app.config将包含您的绑定信息和端点。