Вызов PHP веб-сервиса из .Net - PullRequest
2 голосов
/ 27 июля 2011

Я создал веб-сервис на PHP и пытаюсь вызвать его из своего кода C #.

Когда я пытаюсь создать прокси с помощью утилиты wsdl

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

Я получаю эти ошибки

Ошибка: не удается найти определение для http://myserver.co.za/sayHello:sayHelloPortType.
Описание услуги с пространством имен http://myserver.co.za/sayHello отсутствует.
Имя параметра: имя

Вот мой код веб-сервиса (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 ]

2 голосов
/ 28 июля 2011

Вот что не так с 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

<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>
0 голосов
/ 28 июля 2011

Почему бы вам не добавить ссылку на веб-сервис через:

щелкните правой кнопкой мыши файл проекта -> добавить веб-ссылку -> введите URL-адрес веб-службы и вуаля!

Это должно создать необходимые записи в Web.config (или App.config) плюс прокси-классы, которые вы будете использовать в своем приложении.

...