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.

 [ServiceContract]
public interface IFoo
{
    [OperationContract]
    FooData GetFooData();
}

[DataContract]
public class FooData
{
    [DataMember]
    public string Data { get; set; }
}

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

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.

     [ServiceContract]
    public interface IFoo
    {
        [OperationContract]
          [WebGet(UriTemplate="GetFooData")]  
        FooData GetFooData();
    }
    
  2. Update the configuration

    1. Add endpointBehaviors.

       <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      

    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.

 </configuration>
  <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 
  /><br>        </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:

image

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