【问题标题】:How do you consume a RESTful WCF service from an ASP project using a service reference?如何使用服务引用从 ASP 项目中使用 RESTful WCF 服务?
【发布时间】:2011-10-04 09:49:14
【问题描述】:

我正在开发一个 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>

【问题讨论】:

  • 您似乎可以访问服务配置,为什么不添加 ws 端点并公开元数据以供使用?

标签: asp.net wcf rest service-reference


【解决方案1】:

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

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

更新:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      无法使用来自任何项目的服务引用的 REST 服务,因为服务引用仅适用于 SOAP 服务。 How to consume REST service.

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-02
        • 1970-01-01
        • 1970-01-01
        • 2013-07-10
        • 1970-01-01
        • 2013-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多