Search code examples
asp.netwcfrestservice-reference

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 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>

Solution

  • 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