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:
Decorate the method with WebGet or WebInvoke attribute (in System.ServiceModel.Web.
[ServiceContract] public interface IFoo { [OperationContract] [WebGet(UriTemplate="GetFooData")] FooData GetFooData(); }
Update the configuration
Add endpointBehaviors.
<endpointBehaviors> <behavior name="web"> <webHttp /> </behavior> </endpointBehaviors>
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). https://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
Anonymous
January 26, 2010
Is there any way to add a REST interface to an existing WCF service that uses an endpoint binding of wsHttpBinding?Anonymous
January 27, 2010
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.