从.Net调用PHP webservice

时间:2011-07-27 15:33:48

标签: c# php web-services wsdl webmatrix

我在PHP中创建了一个Web服务,我试图用我的C#代码调用它。

当我尝试使用wsdl实用程序

创建代理时
  

wsdl http://localhost:5365/DemoService.php?wsdl

我收到此错误

  

错误:无法找到http://myserver.co.za/sayHello:sayHelloPortType的定义   缺少名称空间为http://myserver.co.za/sayHello的服务描述   参数名称:名称

这是我的Webservice代码(DemoService.php)

<?php

    function sayHello($name){
        $salutation = "Hi $name !";
        return $salutation;
    }

    $server = new SoapServer("greetings.wsdl");
    $server->addFunction("sayHello");
    $server->handle();

?>

和我的WSDL代码(greetings.wsdl)

<?xml version ='1.0' encoding ='UTF-8' ?> 

<definitions name='greetings' 
  targetNamespace='http://myserver.co.za/sayHello' 
  xmlns:tns=' http://myserver.co.za/sayHello' 
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
  xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' 
  xmlns='http://schemas.xmlsoap.org/wsdl/'> 

  <message name='sayHelloRequest'> 
    <part name='name' type='xsd:string'/> 
  </message> 

  <message name='sayHelloResponse'> 
    <part name='salutation' type='xsd:string'/> 
  </message> 

  <portType name='sayHelloPortType'> 
    <operation name='sayHello'> 
      <input message='tns:sayHelloRequest'/> 
      <output message='tns:sayHelloResponse'/> 
    </operation> 
  </portType> 

  <binding name='sayHelloBinding' type='tns:sayHelloPortType'> 
    <soap:binding style='rpc' 
      transport='http://schemas.xmlsoap.org/soap/http'/> 
    <operation name='sayHello'> 
      <soap:operation soapAction=''/> 
      <input> 
        <soap:body use='encoded' namespace='' 
          encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </input> 
      <output> 
        <soap:body use='encoded' namespace='' 
          encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </output> 
    </operation> 
  </binding> 

  <documentation>This is Wiley's SOAP server Example</documentation>

  <service name='sayHelloService'> 
    <port name='sayHelloPort' binding='sayHelloBinding'> 
      <soap:address location='http://localhost:5365/DemoService.php'/> 
    </port> 
  </service> 

</definitions>

我真的不明白它想说的是什么。有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:2)

这是WSDL的错误

首先xmlns:tns命名空间在其开头有空格

 xmlns:tns=' http://myserver.co.za/sayHello' <-- Bad
 xmlns:tns='http://myserver.co.za/sayHello'  <-- Good

接下来<documentation>节点位置错误,它应该在<service>节点内,如此

<service ...>
    <documentation>This is Wiley's SOAP server Example</documentation>
    <port ...>
        ...
    </port>
</service>

您的端口绑定元素需要使用tns命名空间

<port name='sayHelloPort' binding='sayHelloBinding'>  <-- Bad
<port name='sayHelloPort' binding='tns:sayHelloBinding'>  <-- Good

最后我无法将soap:body导入为encoded,并且必须将它们交换为literal,同时请注意它们需要namespace元素中的值< / p>

<soap:body use='literal' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 

我相信soapAction节点中的<soap:operation soapAction=''/>元素仍然需要一个值才能正常工作,例如urn:xmethods-delayed-quotes#sayHello,但它会在没有它的情况下导入。

完整的WSDL(我可以使用WSDL.exe导入它而不会出错)

<?xml version ='1.0' encoding ='UTF-8' ?> 
<definitions name='greetings' 
  targetNamespace='http://myserver.co.za/sayHello' 
  xmlns:tns='http://myserver.co.za/sayHello' 
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
  xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' 
  xmlns='http://schemas.xmlsoap.org/wsdl/'> 

  <message name='sayHelloRequest'> 
    <part name='name1' type='xsd:string'/> 
  </message> 

  <message name='sayHelloResponse'> 
    <part name='salutation' type='xsd:string'/> 
  </message> 

  <portType name='sayHelloPortType'> 
    <operation name='sayHello'> 
      <input message='tns:sayHelloRequest'/> 
      <output message='tns:sayHelloResponse'/> 
    </operation> 
  </portType> 

  <binding name='sayHelloBinding' type='tns:sayHelloPortType'> 
    <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> 
    <operation name='sayHello'> 
      <soap:operation soapAction=''/> 
      <input>  
        <soap:body use='literal' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </input> 
      <output> 
        <soap:body use='literal' namespace='urn:xmethods-delayed-quotes'  encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </output> 
    </operation> 
  </binding> 
  <service name='sayHelloService'> 
    <documentation>Service Description</documentation>
    <port name='sayHelloPort' binding='tns:sayHelloBinding'> 
      <soap:address location='http://localhost:5365/DemoService.php'/> 
    </port> 
  </service> 
</definitions>

答案 1 :(得分:0)

为什么不通过以下方式添加对Web服务的引用:

右键单击项目文件 - &gt;添加网络参考 - &gt;输入网络服务的网址,瞧!

这应该在Web.config(或App.config)中创建必要的条目以及您将在应用程序中使用的代理类。

相关问题