0

我正在开发一个 RESTful WCF 服务,然后我想从一个单独的 ASP.net 项目中使用它。

是否可以使用 ASP 项目中的服务引用来使用 REST 使用服务,或者是否所有服务引用都被视为 SOAP?

有很多使用服务库作为服务参考或使用 WCF 入门工具包使用 HttpClient 来使用 REST 服务的示例,但我还没有找到一个可以做我希望做的事情。

以下是添加服务引用时自动生成的 ASP.Net web.config 文件的摘录。如您所见,它提到了 SOAP。

  <system.serviceModel>
  <bindings>
   <customBinding>
    <binding name="WebHttpBinding_IDataCaptureService">
     <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
      messageVersion="Soap12" writeEncoding="utf-8">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
       maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     </textMessageEncoding>
    </binding>
   </customBinding>
  </bindings>
  <client>
   <endpoint binding="customBinding" bindingConfiguration="WebHttpBinding_IDataCaptureService"
    contract="testRef.IDataCaptureService" name="WebHttpBinding_IDataCaptureService" />
  </client>
 </system.serviceModel>

这是服务 web.config 的摘录

<system.serviceModel>
    <services>
        <service behaviorConfiguration="DataCaptureService.Service1Behavior" name="eCRB.Service.DataCapture">
            <endpoint address="" behaviorConfiguration="webBehaviour" binding="webHttpBinding" bindingConfiguration="" contract="eCRB.Service.IDataCaptureService">
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <endpointBehaviors>
            <behavior name="webBehaviour">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="DataCaptureService.Service1Behavior">
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true"/>
                <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <bindings>
        <webHttpBinding>
            <binding name="webBinding">
                <security mode="None">
                    <transport clientCredentialType="None"/>
                </security>
            </binding>
        </webHttpBinding>
    </bindings>
</system.serviceModel>
4

3 回答 3

3

看看 ADO.Net 数据服务,它专门支持 REST API,并将以 XML 以外的格式返回数据,例如 JSON

http://msdn.microsoft.com/en-us/data/bb931106

更新:

我看到这是现在重新命名的 WCF 数据服务

于 2011-07-13T11:20:21.533 回答
2

在大多数情况下你不能。原因是 REST 服务没有公开任何 VS2010 可以用来添加服务引用的标准化元数据。我确实说了大部分,那是因为 WCF 数据服务,或者更准确地说是 OData,确实公开了元数据,并且允许您添加服务引用。

于 2011-07-13T11:15:56.407 回答
1

不能使用来自任何项目的服务引用来使用 REST 服务,因为服务引用仅适用于 SOAP 服务。如何使用 REST 服务

HttpClient不是任何当前 WCF 版本的一部分。它包含在 REST Starter Kit 中,它只是社区预览版,从未达到生产最终版本,现在它包含在Web-API中,这是来自未来 WCF 版本的 REST 组件的 CTP。

于 2011-07-13T11:13:34.790 回答