如何使用服务引用从 ASP 项目使用 RESTful WCF 服务?
我正在开发一个 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>
I am developing a RESTful WCF service which I then want to consume from a separate ASP.net Project.
Is it possible to use a service reference from within the ASP project to consume the service using REST or are all service references treated as SOAP?
There are plenty of examples of using a service library as a service reference or consuming a REST service using the WCF starter toolkit using HttpClient but I've not found one that does what I was hoping to do.
Below is an extract from the ASP.Net web.config file that gets auto generated when the service reference is added. As you can see it mentions 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>
Here is an extract from the service 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下 ADO.Net 数据服务,它专门支持 REST API,并且将以 XML 以外的格式返回数据,例如 JSON
http://msdn.microsoft.com/en-us/data/bb931106
更新:
我看到这是现在重新命名的 WCF 数据服务
Take a look at ADO.Net data services, which specifically supports rest API's, and will return data in formats other than XML e.g. JSON
http://msdn.microsoft.com/en-us/data/bb931106
UPDATE:
I see this is now re-branded WCF data services
大多数情况下你不能。原因是 REST 服务不公开 VS2010 可用于添加服务引用的任何标准化元数据。我确实说了大部分内容,这是因为 WCF 数据服务(或更准确地说是 OData)确实公开了元数据,并且允许您添加服务引用。
For the most part you can't. The reason is that a REST service doesn't expose any standardised metadata that VS2010 can use to add a service reference. I did say for the most part and that is because WCF Data Services, or OData to be more exact, does expose metadata and will allow you to do an add service reference.
无法通过任何项目的服务引用来使用 REST 服务,因为服务引用仅适用于 SOAP 服务。 如何使用 REST 服务。
HttpClient
不属于任何当前 WCF 版本。它包含在 REST 入门工具包中,该工具包只是社区预览版,从未达到生产最终版本,现在它包含在 Web-API 是未来 WCF 版本中 REST 组件的 CTP。It is not possible to consume REST service with service reference from any project because service reference is only for SOAP services. How to consume REST service.
HttpClient
is not part of any current WCF version. It was included in REST Starter Kit which was only community preview and never reached production final version and now it is included in Web-API which is CTP of REST components from future WCF version.