22
CRUD Operations using WCF RESTful Service – Part 1 Suttipong Kullawattana

CRUD Operations using WCF RESTful Service – Part1

Embed Size (px)

Citation preview

Page 1: CRUD Operations using WCF RESTful Service – Part1

CRUD Operations using WCF RESTful Service –

Part 1Suttipong Kullawattana

Page 2: CRUD Operations using WCF RESTful Service – Part1

Introduction• REST (Representational State Transfer) is basically an architectural

style that is based on the same principles as that of “Web”. In this WCF Service tutorial we are going to see these web principles in action. We will be using standard HTTP verbs for CRUD (Create, Retrieve, Update, Delete) operations, HTTP Content-Types etc.In one of my previous articles, I explained 5 simple steps to create your first RESTful web service. In part-1 of this article, we will build a RESTful web serviceusingWCF while in second part, we will be consuming the web service using jQuery.

Page 3: CRUD Operations using WCF RESTful Service – Part1

Let’s start by creating a WCF Service Application Project using Visual Studio. For the purpose of this implementation, I am using Visual Studio 2013.

• Open Visual Studio 2013, From File -> New Project -> WCF Service Application.

Page 4: CRUD Operations using WCF RESTful Service – Part1

Before creating WCF REST Service, let’s prepare object that represents data in our application i.e. DataContract class. Right click on newly created project in Visual Studio 2013 and go to Add -> Class and name it as “Books.cs”.

Page 5: CRUD Operations using WCF RESTful Service – Part1

•Don’t forget to add System.ServiceModel and System.Runtime.Serialization as mentioned above.

•Also note that we have created WCF DataContract class “Book” but the file name is “Books.cs”. Although it doesn’t make any difference but just clarifying as we will be adding below BookRepository to same physical file (Books.cs).

Page 6: CRUD Operations using WCF RESTful Service – Part1

For the purpose of simplicity, we are not going to write code for interacting with database. Instead we will separate code for actual CRUD operations using repository design pattern. Benefit of using repository pattern approach is that we can easily replace the implementation with actual database interaction code according to our own need.

00

Page 7: CRUD Operations using WCF RESTful Service – Part1
Page 8: CRUD Operations using WCF RESTful Service – Part1
Page 9: CRUD Operations using WCF RESTful Service – Part1

As we are going to perform CRUD operations using RESTful service, we must know that how these actions map to standard HTTP verbs.

Page 10: CRUD Operations using WCF RESTful Service – Part1

Now, it’s time to add WCF Service to our project. Simply right click on project in Visual Studio 2013 and go to Add -> WCF Service as shown in below screenshot and name our service as “BookService“.

Page 11: CRUD Operations using WCF RESTful Service – Part1

It will add two new files to project: IBookService.cs and BookService.svcFollowing is the code for WCF Service Contract i.e. IBookService:

Page 12: CRUD Operations using WCF RESTful Service – Part1

• Service exposes methods for all CRUD operations. Major things to understand about above code are:• WebInvoke attribute is used to expose services using HTTP verbs like GET, POST, PUT, DELETE etc.

• Method is the HTTP verb used for a specific action already explained above.• RequestFormat is the request message format e.g. JSON, XML etc.• ResponseFormat is the response message format e.g. JSON, XML etc.• UriTemplate is the unique URI for each specific service

operations. Service consumer only knows about unique URI defined in UriTemplate.

Page 13: CRUD Operations using WCF RESTful Service – Part1
Page 14: CRUD Operations using WCF RESTful Service – Part1

Above screenshot clearly show the BookService.svc implementing IBookService interface with GET methods implementation using our repository created earlier.

• GetBookList() returns list of books, while• GetBookById() returns a single Book by given id.

Code for GET methods is self-explainatory.• Now, In order to Add a new Book or update an existing Book record,

we will implement AddBook & UpdateBook WCF REST Service methods as follows:

Page 15: CRUD Operations using WCF RESTful Service – Part1
Page 16: CRUD Operations using WCF RESTful Service – Part1

So the implementation for all CRUD (Create, Retrieve, Update, Delete) operations of our WCF RESTful Service is complete now. We will do the required configuration settings for our REST based Service.

Configuration settings for the service given below. I have placed the complete configuration for <system.serviceModel>.

Page 17: CRUD Operations using WCF RESTful Service – Part1

Above configuration is pretty similar like normal WCF service except the following.

• webHttpBinding is the binding specifically introduced for RESTful services in Windows Communication Foundation version 3.5.• endpointBehavior is “webHttp” for RESTful service.

We are done with implementation of all CRUD(Create, Retrieve, Update, Delete) operations for a WCF RESTful service. Let’s just verify by right clicking on BookService.svc and press “View in Browser“, we will get the following browser window.

Page 18: CRUD Operations using WCF RESTful Service – Part1
Page 19: CRUD Operations using WCF RESTful Service – Part1

We will consume this service by calling all service methods through jQuery code in Part-2 of this WCF REST Tutorial but lets see the results for few calls here to understand the configuration settings and other related concepts.

For example, In order to get all book records, we will modify the URL in browser window as follows:

Page 20: CRUD Operations using WCF RESTful Service – Part1

As you can see that we modify the browser URL with “http://localhost:XXX/BookService.svc/Books” and it displays for download of Books.json at the bottom of the browser window. If we press the Open button to see the contents of Books.json, it will be something as below:

Page 21: CRUD Operations using WCF RESTful Service – Part1

• You can see the results from WCF RESTful Service in JSON format of the data we put in our repository class.• As we discussed earlier about WebInvoke attributes

of IBookService interface, we can conclude few points here as:• Name of our GET method was GetBooksList() but in WebInvoke attribute

UriTemplate, we provided the URI as “Books/” means we will access the RESTful service as:http://localhost:xxxx/BookService.svc/Books/• ResponseFormat = WebMessageFormat.Json, so output is JSON format. If

we modify and change it to WebMessageFormat.Xml, the same method call will output in XML format as follows:

Page 22: CRUD Operations using WCF RESTful Service – Part1