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.
I have done some basic editing to the generated code, so the code now looks like this-IMyservice.cs"
namespace Wcf_Test
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetData();
}
}
MyService.cs: Basically for easy understanding i have just returned a string value.
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
.
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
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.
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