Exposing WCF REST Interface, Step by Step

It is really hard for me to think another way to build services, other than WCF. WCF team did a very good job handling all the necessary plumbing.

This post shows a quick tutorial on how to expose a WCF service as a REST service, using IIS. This post assumes that there is already a working service called Foo, with one method that returns FooData. Just add a WCF service within Visual Studio.

public interface IFoo
    FooData GetFooData();

public class FooData
    public string Data { get; set; }

public class Foo : IFoo
    public FooData GetFooData()
        return new FooData() { Data = "You Had Me at Hello World." };
      <service behaviorConfiguration="FooBehavior" name="Foo">
        <endpoint address="" binding="wsHttpBinding" contract="IFoo">
            <dns value="localhost" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <behavior name="FooBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />

To expose a REST interface of that service, these are the steps:

  1. Decorate the method with WebGet or WebInvoke attribute (in System.ServiceModel.Web.
    public interface IFoo
        FooData GetFooData();
  2. Update the configuration
    1. Add endpointBehaviors.
        <behavior name="web">
          <webHttp />

    2. Update the endPoint by changing the binding to webHttpBinding, and add behaviorConfiguration to the endPoint node to point to the endpointBehavior from step 1.
      <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="IFoo">

At the end, this is the final configuration looks like.

<system.serviceModel> <services> <service behaviorConfiguration="FooBehavior" name="Foo"> <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="IFoo"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <endpointBehaviors> <behavior name="web"> <webHttp /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="FooBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>

It is done. Now open your browser and point it to your REST url (the .svc uri + the method name as specified in UriTemplate attribute (GetFooData in this example). http://localhost:8081/Foo.svc/GetFooData, and you will get:


That’s it. Two simple steps, and you have a REST interface for your WCF service.

Comments (2)
  1. Marc S. says:

    Is there any way to add a REST interface to an existing WCF service that uses an endpoint binding of wsHttpBinding?

  2. mhsimkin says:

    I meant, is there anyway to add an additional binding for REST to an existing wsHttpBinding service.  I don’t want to replace the existing, just offer it as both SOAP and REST.

Comments are closed.

Skip to main content