【问题标题】:Create a web service with XML response使用 XML 响应创建 Web 服务
【发布时间】:2010-10-04 10:36:25
【问题描述】:

使用 XML 响应创建 Web 服务的最简单方法是什么?

  1. 使用 WCF 创建 Web 服务? (看起来很复杂)
  2. 如果我想使用 WCF 创建我的 Web 服务,我应该从哪里开始?

【问题讨论】:

标签: c# asp.net xml wcf web-services


【解决方案1】:

在你的情况下,我肯定会使用 WCF 和 REST 绑定 (webHttpBinding) - 我不同意它学习起来很复杂。

查看这些资源以开始使用:

【讨论】:

    【解决方案2】:

    本文中提供的链接很少。希望他们会帮助你-

    http://social.msdn.microsoft.com/Forums/en/wcf/thread/b082d6de-d1e9-4e51-a0ab-0fe98d7003e6

    【讨论】:

      【解决方案3】:

      使用 XML 响应创建 Web 服务的最简单方法是,毫无疑问,将 XML 文件放在标准 Web 服务器上并将其作为静态文件提供。

      不过,我猜你想要更灵活的东西......

      您有多种选择,而 WCF 处于更复杂(但更灵活)的范围内。第一个问题:你的客户是什么?你在写吗?您想编写一个可供其他客户端使用的 Web 服务吗?

      您想使用 REST——即普通旧 XML (POX) 而非普通旧 HTTP? XML-RPC?肥皂?

      WCF 支持所有这些,因此这实际上取决于您要支持哪些客户端。

      更新:如果你想支持 XML-RPC,你可以做的比从this implementation of XML-RPC for WCF by Clemens Vasters 开始更糟糕。我问了一个关于这个here的问题。

      【讨论】:

      • 我知道 WCF 支持所有这些,而且它很灵活,但它非常复杂,比简单地创建一个 SOAP Web 服务要复杂得多。
      • 我只想创建一个 XML-RPC Web 服务,没有 SOAP(开销太大)。
      【解决方案4】:

      创建 WCF 服务实际上非常容易。网上有很多教程。

      至于返回xml,有几种方法。 您可以通过将 xml 转换为服务中的字符串,然后在客户端中转换回“老派” SOAP Web 服务来执行此操作。它不漂亮,但很有效。

      另一种方法,也是我的做法,是使用 WCF 并创建一个映射您的 xml 的数据协定。

      您可以使用数据协定做一些非常好的事情,例如传递数据集和自定义类型,但这有时会限制您可以使用的绑定类型。

      【讨论】:

        【解决方案5】:

        我刚刚做了一个网络服务。

        PHP服务器端代码:

        <?php // instantiate SOAP server
        function sendXmlMsg($msg){
        return $msg;
        }
        ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSD
        $server = new SoapServer("mark.wsdl");
        // Register exposed method
        $server->addFunction('sendXmlMsg'); // generate captcha
        //$server->addFunction('check_captcha'); // check captcha ID
        $server->handle(); //?>
        

        我的 WSDL 文件是

        <?xml version ='1.0' encoding ='UTF-8' ?>
        <definitions name='Msg91'
          targetNamespace='http://localhost/webtest/test.wsdl'
          xmlns:tns='http://localhost/webtest/test.wsdl'
          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='sendXmlMsgRequest'>
          <part name='msg' type='xsd:string'/>
        </message>
        <message name='sendXmlMsgResponse'>
          <part name='Result' type='xsd:string'/>
        </message>
        <portType name='Msg91PortType'>
          <operation name='sendXmlMsg'>
            <input message='tns:sendXmlMsgRequest'/>
            <output message='tns:sendXmlMsgResponse'/>
          </operation>
        </portType>
        
        <binding name='Msg91Binding' type='tns:Msg91PortType'>
          <soap:binding style='rpc'
            transport='http://schemas.xmlsoap.org/soap/http'/>
            <operation name='sendXmlMsg'>
            <soap:operation soapAction='urn:xmethods-delayed-quotes#sendXmlMsg'/>
            <input>
              <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
                encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
            </input>
            <output>
              <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
                encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
            </output>
          </operation>
        </binding>
        
        <service name='Msg91Service'>
          <port name='Msg91Port' binding='tns:Msg91Binding'>
            <soap:address location='http://localhost/webtest/test.php'/>
          </port>
        </service>
        </definitions>
        

        客户端 PHP 文件:

        <?php
        $client = new SoapClient("mark.wsdl");
        $params= array('HiT');
        echo $client->__soapCall( 'sendXmlMsg', $params );
        ?>
        

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-28
          • 1970-01-01
          • 2021-11-29
          • 2011-11-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多