Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CRUD operations using WCF RESTful service – part2

138 views

Published on

CRUD operations using WCF RESTful service – part2

Published in: Education
  • Be the first to comment

  • Be the first to like this

CRUD operations using WCF RESTful service – part2

  1. 1. CRUD Operations using WCF RESTful Service – Part 2 Suttipong Kullawattana
  2. 2. 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.
  3. 3. • 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.
  4. 4. • 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.
  5. 5. • 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.
  6. 6. 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:

×