In this Step by step we Make a WCF RESTful Web Service with CRUD operations in 5 minutes
we see how to build a WCF REST web service with all CRUD operations to be Ajax called, all this in just 5 minutes, following this simple steps :1) Use Visual Studio templates and build an Ajax WCF web service
2) Customize it to support RESTful calls in JSON format (can also be XML)
3) Code all C.R.U.D. (Create Read Update Delete) operations
The whole code for this tutorial , can be downloaded from the following GitHub repository:
https://github.com/CarmelSoftware/WCF_RESTful_WebService
To build this service, we use a demo class which will be exposed by the REST service through HTTP calls, that you can later replace with your own EDM data model classes.
This an HTTP GET response from this RESTful WCF service :
Make a WCF RESTful Web Service with CRUD operations in 5 minutes
The whole process for making this REST service , is as follows :
1) Use Visual Studio templates and build an Ajax WCF web service:
First build an ASP.NET Web Application using this Visual Studio template:
Then, add an AJAX enabled WCF web service to the project:
To make run this service, we design a demo class called "Data", which will be exposed by the REST service through HTTP calls:
You'll get the following usable skeleton for the WCF service:
Take a look at the web.config file to see the WCF service configuration:
As you can see , there is an endpoint which wears the name of the Web service class.
The "enableWebScript " behavior sets that the service will respond to AJAX calls.
2) Customize it to support RESTful calls in JSON format (can also be XML)
First thing we do, is to change the endpoint behavior to turn it into a REST service, using the "webHttp" directive, as follows:
Notice that this directive replaces the "enableWebScript" one.
This data will be decorated with the DataContract directive, and the properties will be DataMembers.
We also add some constructors to create some records and return them by the service.
After you have your WCF RESTful service working, you will replace this demo class with some EDM data model classes of your own.
Remember to declare "serializable" your model.
3) Code all C.R.U.D. (Create Read Update Delete) operations
Then go to the WCF class and replace the "DoWork" method with the HTTP GET method to return ALL items:
HTTP GET :
HTTP PATCH :
The HTTP PATCH verb is used in REST for updating just some of the object's fields:
HTTP GET :
We specify the handle for HTTP GET requests, by using the attribute "WebGet".
Also, we stipulate the response format as JSON, since the default is XML.
And we set an UriTemplate as "/Get" , which will differentiate this method from the second GET one, which will return only one item according to the ID, which you will code as follows:
Notice that the return value is an array of Data objects and a single Data object, accordingly:
HTTP POST :
The POST HTTP verb is used to CREATE a new item, and we define it using the WebInvoke attribute:
We only want to check that our REST service works, so that just return the same Data object from the request (here i'm using POSTMAN to send HTTP requests. You can also use FIDDLER for that):
Important: the JSON object in the request's body must hold the field's data with exactly the same field names set in the DataMembers in your service: it is case-sensitive ("Id" means "Id", and no "id", got it?).
HTTP PUT:
The PUT HTTP VERB is used to update the entire item, i.e. to replace it with an updated one, and we do so by using the WebInvoke PUT option as follows:
The binder will give you the Date object from the request, if the names of the fields being the same as the class in a case-sensitive way, and also will give you the ID of the item to update. Check that you wrote EXACTLY the same variable name for this : "Put/{id}" == string "id" .
Test this method from POSTMAN sending an PUT request with a JSON object in the body:
Important: the JSON object inside the request's body must contain the field's data with exactly the same field names set in the DataMembers in your service: it is case-sensitive ("Id" means "Id", and no "id").
HTTP PATCH :
The HTTP PATCH verb is used in REST for updating just some of the object's fields:
Again, test it using this REST call with the HTTP PATCH verb:
HTTP DELETE :
Finally, create the method to handle HTTP DELETE requests, as follows:
THE END
That's all. Now you have a working WCF RESTful web service , and you can replace the Data model with your own.
by Carmel Schvartzman
כתב: כרמל שוורצמן
No comments:
Post a Comment