crud operations using wcf restful service – part2

11

Click here to load reader

Upload: suttipong-kullawattana

Post on 21-Feb-2017

19 views

Category:

Education


6 download

TRANSCRIPT

Page 1: CRUD operations using WCF RESTful service – part2

CRUD Operations using WCF RESTful Service –

Part 2Suttipong Kullawattana

Page 2: CRUD operations using WCF RESTful service – part2

Introduction• In previous part of this WCF Tutorial, we created a WCF RESTful service step by

step and make it available to be consumed by a client. Here in this part, we will write and understand code to consume the RESTful web service using jQuery. Same like previous part, I will write complete jQuery code for all CRUD operations instead of leaving something for the reader. One of the advantages of using REST based service is that it can be consumed by a wide range of client applications.• Following is the jQuery code for each CRUD operation call against

WCF RESTful service created in part 1 of this WCF Application Tutorial series. For the purpose of implementation, I have created a new ASP.NET project to existing solution and add an ASP.NET Web Form to make calls. But you can have your own choice of client implementation using following jQuery code.

Page 3: CRUD operations using WCF RESTful service – part2

• Note: For this implementation, my focus is to provide you with jQuery code for calling a WCF RESTful Service for all CRUD (Create | Retrieve | Update | Delete) operations. So to avoid UI complications, I am preparing the data object for CREATE and UPDATE operations inside the code instead of using User Interface form. You can implement that in your own way and according to your need but jQuery AJAX call for all CRUD operations to WCF REST based Service will remain same.

Page 4: CRUD operations using WCF RESTful service – part2
Page 5: CRUD operations using WCF RESTful service – part2
Page 6: CRUD operations using WCF RESTful service – part2

• jQuery asynchronous HTTP Request is made with type as “GET”. It represents HTTP verb being used to make a call. We have used same verb on WebInvoke attribute while defining service contract for GetBookList service method.• Second important thing is

URL”http://localhost/RESTServiceCRUD/BookService.svc/Books/”.In order to understand it, we can break up above URL into two parts as:• http://localhost/RESTServiceCRUD/BookService.svc is simple URL to our

service file (.svc).• /Books/  is what we define as UriTemplate in WebInvoke attribute of that

particular method.

Page 7: CRUD operations using WCF RESTful service – part2

• Note: You may have noticed that for in Part-1, method name in service contract was “GetBookList” but UriTemplate was “Books/”. So, client only understand UriTemplate not the exact method name.• Third and last input parameter is “Content-Type”. It’s sent with

request header to tell server that what kind of response is expected on client-side i.e. JSON.• On success, the output will be in the form of JSON, so just parse it and

append rows to already entered html table.• “GetBookById” is another way of consuming RESTful service and

getting a record by bookId.

Page 8: CRUD operations using WCF RESTful service – part2
Page 9: CRUD operations using WCF RESTful service – part2

In this case the URL is “http://localhost/RESTServiceCRUD/BookService.svc/BookById/2″. UriTemplate is “BookById” for service method “GetBookById” and “2” is the parameter for bookId.Implementation for adding a new book or updating an existing record is as follows:

Page 10: CRUD operations using WCF RESTful service – part2
Page 11: CRUD operations using WCF RESTful service – part2