Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

First Step to REST via WCF

Rate me:
Please Sign up or sign in to vote.
3.08/5 (6 votes)
27 Sep 2011CPOL2 min read 21.5K   17   3
Steps to implement RESTful Web service through WCF

Introduction

To add to what we have seen in my earlier post on REST. This article gives you an understanding on how REST could be implemented via WCF.  

Let's Start 

We will start by creating a WCF Service Application project

Once the project is created, you can see IService.cs followed by Service.svc which has Service.svc.cs.

Now what i have done is that i have renamed the inteface to IMyService and the service to MyService.

Wcf_Test_Sol_Expl.JPG

I have done some basic editing to the generated code, so the code now looks like this-IMyservice.cs"

C#
namespace Wcf_Test
{
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string GetData();
    }
}

MyService.cs: Basically for easy understanding i have just returned a string value.

C#
public class MyService : IMyService
{
    public string GetData()
    {
        return "Some Value from Server";
    }
}

Implementing REST in WCF

To implement REST, all we need to do is to tweak the IMyService.

C#
namespace Wcf_Test
{
	[ServiceContract]
	public interface IMyService
	{
		[OperationContract]
		[WebGet(UriTemplate = "GetMeValues", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
		string GetData();
	}
}

To get the WebGet, you will need to have use the following namespace references

  • System.ServiceModel
  • System.ServiceModel.Web

Now all set and done, the most important is the web.config change.

REST services cannot work on ws or basic httpBinding, it can only work on WebHttpBinding. So the end point should definetly have a binding as WebHttpBinding.

<endpoint address="" binding="webHttpBinding"contract="Wcf_Test.IMyService" behaviorConfiguration="httpEndpointBehavour"> 
<identity> 
     <dns value="localhost"/> 
<Identity>  
</endpoint> 

You will also have to define the behaviour.

<behaviors> <serviceBehaviors> 
<behavior name="httpBehaviour"> <serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior></serviceBehaviors>

One thing that should not be forgotten is declaring the endpointbehviour

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

Once set and done, you are ready to hit F5, to see for yourself the first Rest implementation in WCF

Result_WithoutREST.JPG

You can have the same o/p even without changing your Config.

All you need to do is delete all the config changes that is stated above in this post. Once done the we have to add a new attribute called Factory to the MyService.svc. See code below.

The MyService.svc contains

ServiceHost Language="C#" Debug="true" Service="Wcf_Test.MyService" 
CodeBehind="MyService.svc.cs"

now add an attribute named Factory.

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

 press F5. And there you go. You will get exacltly the same o/p without making any change to your Config files.

Result_WithREST__1_.JPG

Conclusion

I hope that this post was usefull for those who wanted to try REST implementation in WCF for the first time. This is just a very simple example i would reckon you to try invoking all the methods like PUT,POST,DELETE.

More Posts available at http://www.bloggingbunk.com  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Tesco
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionok Pin
Sacha Barber12-Sep-11 4:06
Sacha Barber12-Sep-11 4:06 
GeneralMy vote of 2 Pin
Sacha Barber12-Sep-11 4:05
Sacha Barber12-Sep-11 4:05 
GeneralRe: My vote of 2 Pin
s.jdm12-Sep-11 18:12
s.jdm12-Sep-11 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.