How do you consume a RESTful WCF service from an ASP project using a service reference?
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 belo开发者_开发问答w 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>
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
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.
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.
精彩评论