Categories :-Introduction to WCF ,
Differences Between WCF and ASP.NET Web Services
Introduction :-
Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. WCF provides a runtime environment for your services, enabling you to expose CLR types as services, and to consume other services as CLR types. In this article, I am going to explain how to implement restful service API using WCF 4.0 . The Created API returns XML and JSON data using WCF attributes.
REST uses some common HTTP methods to insert/delete/update/retrieve information which is below:
- GET - Requests a specific representation of a resource
- PUT - Creates or updates a resource with the supplied representation
- DELETE - Deletes the specified resource
- POST - Submits data to be processed by the identified resource
Why and Where to Use REST?
- Less overhead (no SOAP envelope to wrap every call in)
- Less duplication (HTTP already represents operations like
DELETE
, PUT
, GET
, etc. that have to otherwise be represented in a SOAP envelope).
- More standardized - HTTP operations are well understood and operate consistently. Some SOAP implementations can get finicky.
- More human readable and testable (harder to test SOAP with just a browser).
- Don't need to use XML (well, you kind of don't have to for SOAP
either but it hardly makes sense since you're already doing parsing of
the envelope).
- Libraries have made SOAP (kind of) easy. But you are abstracting
away a lot of redundancy underneath as I have noted. Yes, in theory,
SOAP can go over other transports so as to avoid riding atop a layer
doing similar things, but in reality just about all SOAP work you'll
ever do is over HTTP.
Step by Step Guide For Develop WCF restful web service
We will develop
Restful WCS API in 6 steps. So let’s start now.
STEP 1
First of all launch Visual Studio 2010. Click
FILE->
NEW->
PROJECT. Create new "
WCF Service Application".
STEP 2
Once you create the project, you can see in solution that By Default
WCF service and interface file are already created. Delete By default
created file as we will create our own interface and WCF service file.
STEP 3
Now right click on solution and create one new WCF service file. I have given name to the service file as “
RestServiceImpl.svc”.
STEP 4
As I explained at the start of the article that we will be writing an
API which can return data in XML and JSON format, here is the interface
for that. In
IRestServiceImpl
, add the following code:
Copy the Above shource Code as shown Below
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "GET",ResponseFormat=System.ServiceModel.Web.WebMessageFormat.Xml, BodyStyle =System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")]
string JSONData(string id);
} |
In the above code, you can see two different methods of
IRestService
which are
XMLData
and
JSONData
.
XMLData
returns result in XML whereas
JSONData
in JSON.
STEP 5
Open the file
RestServiceImpl.svc.cs and write the following code over there:
Find the Source as shown below
public class RestServiceImpl : IRestServiceImpl
{
#region IRestService Members
public string XMLData(string id)
{
return "You Request Porduct" + ":"+id;
}
public string JSONData(string id)
{
return "Yor Request Product" +":"+ id;
}
#endregion
} |
STEP 6
Now let’s move to configuration part which is the last one. There
will be two basic parts of the configurations file which we must have to
understand.
<services>
This part contains information about the End Point. Below are the code details.
<behaviors>
This part contains details about service and endpoint behavior.
Source code of web.config file as shown below
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="metadataBehavior">
<endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration> |
Build your Project Which Shown Below
You got -:
Service added Succefully in bottom of left hand site of the corner
Output
In xml and json format as shown below
Xml Format
JSON Format
Download sample code attached