c# - 从 .Net 调用 PHP 网络服务

标签 c# php web-services wsdl webmatrix

我已经在 PHP 中创建了一个 Web 服务,我正在尝试从我的 C# 代码中调用它。

当我尝试使用 wsdl 实用程序创建代理时

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

我得到了这个错误

Error: Cannot find definition for http://myserver.co.za/sayHello:sayHelloPortType.
Service Description with namespace http://myserver.co.za/sayHello is missing.
Parameter name: name

这是我的 Web 服务代码 (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>

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

最佳答案

这是 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>

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

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

最后我无法得到 soap:body导入为 encoded , 并且不得不将它们交换为 literal , 还要注意他们需要 namespace 中的值元素

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

我相信soapAction <soap:operation soapAction=''/> 中的元素node 仍然需要一个值才能正常工作,比如 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>

关于c# - 从 .Net 调用 PHP 网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6847100/

相关文章:

c# - sosex.mbp 或 sosex.mbm 设置的断点不起作用

c# - Mvvm-light/Serilog 设计时错误 : Could not load file or assembly [VS2017]

c# - 部署 ASP.NET Web 应用程序

PHP——如何从表中选择随机但不同的行

javascript - 使用 Ajax post 调用的 php 脚本可以在脚本执行时发回 SSE 事件吗

wcf - 无法验证消息中的至少一个安全 token

android - 使用 edittext 和按钮从 android eclipse 使用 web 服务

c# - 我可以将 pocos 与 mongodb c# 驱动程序一起使用吗

php - PSR-4:自动加载器( Composer )和扩展命名空间确保后备 php

wcf - 网络服务 : Ruby on Rails versus WCF