5 simple steps to create your first RESTful

  • Published on
    21-Feb-2017

  • View
    12

  • Download
    0

Embed Size (px)

Transcript

5 simple steps to create your first RESTful service

5 simple steps to create your first RESTful serviceSuttipong Kullawattana : Java Developer at CWN

IntroductionRESTful services are those which follow the REST (Representational State Transfer) architectural style. Before implementing your first RESTful service, lets first understand the concept behind it. As we know that WCF allows us to make calls and exchange messages using SOAP over a variety of protocols i.e. HTTP, TCP, Named Pipes and MSMQ etc. In a scenario, if we are using SOAP over HTTP, we are just utilizing HTTP as a transport. But HTTP is much more than just a transport. So, when we talk about REST architectural style, it dictates that Instead of using complex mechanisms like CORBA, RPC or SOAP for communication, simply HTTP should be used for making calls.

::::: Practical Guide To WCF RESTful Service :::::RESTful architecture use HTTP for all CRUD operations like (Read/Create/Update/Delete) using simple HTTP verbs like (GET, POST, PUT, and DELETE).Its simple as well as lightweight. For the sake of simplicity, I am going to implement only a GET request for which service will return certain types of data (i.e. Product data) in XML format.

Following are 5 simple steps to create your first RESTful service that returns data in XML.Create a WCF Service Project.Preparing the data (e.g. Product) to returnCreating Service ContractImplementing ServiceConfigure Service and Behavior

1. Create a WCF Service ProjectOpen Visual Studio.FromFile -> New Project. Select WCF from left and create a newWCF Service Application.

2. Preparing the data to returnNow add a class to newly created project. Name it to Products.cs.

Now this Products.cs file will contain two things.First one is Data Contract as follows:

Second one is a singleton implemented class that gets products data from a database and return list of products. In order to make it simple, we are preparing data inside this class instead of fetching from the database as follows:

3. Creating Service ContractNow add a new WCF Service to this project as follows:

It will add contract as well as service file to project. Following is the code for service contract i.e. IProductRESTService.cs.

IProductRESTServicecontains only one method i.e. GetProductList. Important points to understand about this method is WebInvoke attribute parameters.Method= GET, represents an HTTP GET request.ResponseFormat= WebMessageFormat.Xml, response format will be XML here but we can return JSON as well by changing its value to WebMessageFormat.json.BodyStyle= WebMessageBodyStyle.Bare, indicates neitherthe request and nor response are wrapped. Other possible values for BodyStyle areWrapped,WrappedRequest,WrappedResponse.UriTemplate= GetProductList/, it has two parts, URL path and query.

4. Implementing RESTful ServiceIn this step we are going to implement the service. Only one method GetProductList is defined in the contract, so implementing service class will be as follows:

5. Configure Service and BehaviorThe last step is to configure the service and its behaviors using the configuration file. Following is the complete ServiceModel configuration settings.

webHTTPBindingis the binding used for RESTful services.

Now, everything about creating RESTful service is done.

You can easily run and test it.

Right click ProductRESTService.svc file and click View in Browser. You will see the following screen, that means service is fine.

An important point to consider here is that inService Behavior Configuration, we have settedhttpGetEnabled=trueforserviceMetadatathats why we are getting above service screen with wsdl option.

This is what we do normally for SOAP-based services. REST-based services only uses HTTP verbs on a resource, so we can disable WSDL in this case by simply settinghttpGetEnabled=false. Now if we run the service again, we will get the following screen.

Just modify the URL in browser and add GetProductList/ to it. So, this is the UriTemplete we defined as service contract method.

Recommended

View more >