This article shows a simple way of developing WCF REST Services and consuming them through JQuery in ASP.Net application.
What is REST?
REST – Representational State Transfer in an architectural style for designing network applications. The main idea behind is that rather using different mechanisms such as SOAP, CORBA, RPC to connect between systems, user can use simple HTTP to make calls between them. RESTful Services use HTTP requests to POST (modify or update) data, to DEL (delete) data or GET(retrieve) data.
What is JQuery?
jQuery is a coding language that is a branch from JavaScript. jQuery works like JavaScript where it’s used to help with interaction and effects with your development code.
You can find the complete tutorial of JQuery at http://docs.jquery.com/Tutorials:How_jQuery_Works
Let’s start the real Coding work.
Creating WCF Rest Service
- Open Visual Studio
- Create a WCF Service Application Project.
-
Create a new Service Interface named as IRestServiceImpl.cs, following code snippet shows the complete code of this interface.
namespace RestService
{
// NOTE: You can use the “Rename” command on the “Refactor” menu to change the interface name “IRestServiceImpl” in both code and config file together.
[ServiceContract]
public
interface
IRestServiceImpl
{
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Xml, BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate= “xml/{id}”)]
string XMLData(string id);
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate=“json/{id}”)]
string JSONData(string id);
}
}
In WCF all the web methods should be annotated with the OperationContract and the service class should be annotated with the ServiceContract. This is necessary for both SOAP based or REST based calls.
Rest, as per the above code there are two WCF operation contracts; one returns data in XML and the other returns data in JSON. JSON data is more less in size as it does not contains tags and all that what xml have. Both the methods are annotated with WebGet attribute.
In WCF for making REST based calls you need to annotate the method either using WebGet or WebInvoke to define specific mapping to the HTTP uniform interface. The reason Microsoft provided two attributes because GET requests are logically different from POST, DELETE, PUT etc. the bottom line is if you want to use HTTP GET you use WebGet otherwise WebInvoke for all others. Here as we are retrieving the data I have used WebGet.ResponseFormat tell what the format of the data that is travel back to client. There are two formats XML or JSON. UriTemplate helps to specify the template of REST calls over HTTP and the way method will be accessible from client in a REST way.
-
Create a RestServiceImpl.cs class and implement IRestServiceImpl.cs interface. Following code shows the complete snippet of RestServiceImpl.cs
namespace RestService
{
// NOTE: You can use the “Rename” command on the “Refactor” menu to change the class name “RestServiceImpl” in code, svc and config file together.
public
class
RestServiceImpl : IRestServiceImpl
{
public
string XMLData(string id)
{
return
“you have typed=” + id;
}
public
string JSONData(string id)
{
return
“you have typed=” + id;
}
}
}
Testing WCF Service
-
Now just run the WCF Service Application and test. Below URL shows the way of making REST calls.
For XML,
http://localhost:31189/RestServiceImpl.svc/xml/123
For JSON,
http://localhost:31189/RestServiceImpl.svc/json/123
Creating ASP.Net Web Application
- Create ASP.Net web project to consume WCF REST service
-
Open default.aspx page and add html text box and button
<input
id=”text” type=”text”/>
<input
id=”set” type=”button” value=”Click” onclick=”RetrieveVIAWCFREST();”/>
-
Now define the function RetrieveVIAWCFREST under script tag
<script>
function RetrieveVIAWCFREST() {
var textVal= $(‘#text’).val();
$.ajax({
type: “GET”,
contentType: “application/json; charset=utf-8”,
url: “http://localhost:31189/RestServiceImpl.svc/json/”+textVal,
dataType: “json”,
processdata: false,
success: function (data) {
var result=data.JSONDataResult;
alert(result);
},
error: function () { alert(“Not Done”); }
});
}
</script>
.ajax is a JQuery function which helps the developer to transport the data from client server in a simple way. It calls XMLHttpRequest object inside and make calls automatically. But developer has to define certain attribute values to make call smoothly.
When the call executes successfully success function will be called and the appropriate action will be executed inside it. On error, error function will be called and the appropriate action will be executed. There is a complete attribute also and user can define a function on complete event. Please check JQuery docs for more information.
Running Web Application
Let’s run the Web application. Enter text in the textbox and click the ‘Click’ button.
You got a very great website, Gladiolus I noticed it through yahoo.
Wow. Good luck to you with that. Your endeavours are certainly blessed by the great one above. Best wishes!
Thank you for some other informative website. Where else may just I am getting that kind of info written in such an ideal way? I’ve a undertaking that I am just now operating on, and I have been at the glance out for such info.
Glad to be one of many visitants on this incredible internet internet site : D.
Its like you learn my thoughts! You appear to grasp a lot approximately this, such as you wrote the ebook in it or something. I feel that you simply can do with some percent to power the message home a little bit, but instead of that, this is fantastic blog. A fantastic read. I will definitely be back.
you are truly a good webmaster. The website loading speed is amazing. It sort of feels that you are doing any unique trick. Moreover, The contents are masterwork. you’ve performed a fantastic activity on this matter!
As I website owner I believe the subject material here is rattling excellent , appreciate it for your efforts.
I became honored to get a call from my friend immediately he discovered the important recommendations shared in your site. Examining your blog article is a real great experience. Many thanks for thinking about readers just like me, and I wish you the best of achievements as a professional in this arena.
I consider something really special in this website.
I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thank you again!
I want to show my gratitude for the very good work. I just don’t know how you been able to makes this interesting, but you did. Thanks for your time.
I really like your writing style, good info, thank you for posting :D. “If a cluttered desk is the sign of a cluttered mind, what is the significance of a clean desk” by Laurence J. Peter.
Wow! This could be one particular of the most useful blogs We’ve ever arrive across on this subject. Basically Excellent. I’m also a specialist in this topic so I can understand your effort.
Some truly prime content on this web site , saved to my bookmarks .
I believe this internet site has some real great info for everyone. “The fewer the words, the better the prayer.” by Martin Luther.
Hey there exceptional website! Does running a blog similar to this require a large amount of work? I have no expertise in programming however I was hoping to start my own blog in the near future. Anyway, if you have any recommendations or techniques for new blog owners please share. I understand this is off subject nevertheless I simply needed to ask. Kudos!
What’s Taking place i’m new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads. I hope to contribute & aid different customers like its helped me. Great job.
I love the efforts you have put in this, thanks for all the great blog posts.
Keep up the great piece of work, I read few articles on this web site and I conceive that your site is rattling interesting and holds lots of excellent info.
You really make it appear really easy with your presentation however I in finding this topic to be actually one thing which I think I might never understand. It sort of feels too complicated and extremely extensive for me. I am having a look ahead for your next post, I will attempt to get the dangle of it!
I like this weblog it’s a master piece! Glad I found this on google.
I always was interested in this subject and stock still am, thank you for posting.
I am so happy to read this. This is the type of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this greatest doc.
I like this site very much so much great information. “Abstract art is a product of the untalented, sold by the unprincipled to the utterly bewildered.” by Al Capp.
Rattling nice style and great articles , absolutely nothing else we want : D.
Very interesting topic, thank you for putting up.
There is clearly a bundle to know about this. I suppose you made various nice points in features also.
Hi there! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept talking about this. I will forward this article to him. Fairly certain he will have a good read. Many thanks for sharing!
But wanna remark on few general things, The website design and style is perfect, the content is real fantastic : D.
Absolutely indited written content , appreciate it for entropy.
A person essentially assist to make critically articles I’d state. That is the very first time I frequented your web page and thus far? I surprised with the research you made to create this actual submit incredible. Magnificent process!
We wish to thank you once again for the wonderful ideas you offered Jeremy when preparing a post-graduate research in addition to, most importantly, regarding providing each of the ideas in a blog post. If we had been aware of your site a year ago, we might have been saved the nonessential measures we were implementing. Thanks to you.
I see something really special in this web site.
Merely a smiling visitor here to share the love (:, btw great design .
Very interesting topic, thank you for putting up.
I would like to thank you for the efforts you have put in writing this web site. I am hoping the same high-grade web site post from you in the upcoming also. Actually your creative writing skills has encouraged me to get my own website now. Actually the blogging is spreading its wings fast. Your write up is a good example of it.
Hello, you used to write wonderful, but the last few posts have been kinda boringÖ I miss your great writings. Past few posts are just a little out of track! come on!
I love the efforts you have put in this, regards for all the great articles.
hello dudes!! gorgeous site!
I think this site contains very superb composed content posts.
Keep up the fantastic piece of work, I read few posts on this internet site and I conceive that your web site is very interesting and has got circles of wonderful info .
As I website possessor I believe the content matter here is rattling fantastic , appreciate it for your hard work. You should keep it up forever! Good Luck.
If anyone found another so good a blog let me know
Utterly written written content , appreciate it for information .
Exceptional post. This was a highly engrossing blog. Thanks!
I was studying some of your posts on this internet site and I believe this web site is real instructive! Keep posting .
Outstanding post, I conceive blog owners should acquire a lot from this site its really user genial . “Choose your pleasures for yourself, and do not let them be imposed upon you.” by Lord Chesterfield.
I enjoy the efforts you have put in this, appreciate it for all the great articles.
Very interesting information!Perfect just what I was searching for!
Wohh exactly what I was looking for, thankyou for posting . “Study men, not historians.” by Harry S Truman.
This is very interesting, You are a very professional blogger. I have joined your feed and look ahead to seeking extra of your magnificent post. Additionally, I have shared your site in my social networks!
Sharing some thing is superior than keeping up-to our self, thus the YouTube video that is posted here I am going to share by means of my family and friends.
You have remarked very interesting points! ps decent site.
Merely to follow up on the up-date of this issue on your website and would really want to let you know just how much I liked the time you took to generate this helpful post. In the post, you spoke regarding how to really handle this matter with all comfort. It would be my own pleasure to build up some more strategies from your site and come as much as offer other individuals what I learned from you. I appreciate your usual good effort.
Some truly nice and utilitarian information on this website, as well I think the style holds great features.
My spouse and I stumbled over here coming from a different web page and thought I may as well check things out. I like what I see so i am just following you. Look forward to looking over your web page repeatedly.
I really enjoy reading through on this internet site , it holds great content . “We find comfort among those who agree with us–growth among those who don’t.” by Frank A. Clark.
I simply wanted to write down a small comment to express gratitude to you for those amazing techniques you are posting here.
We would like to thank you once again for the gorgeous ideas you gave Jeremy when preparing her post-graduate research and, most importantly, pertaining to providing all of the ideas in one blog post. If we had known of your web page a year ago, we would have been saved the pointless measures we were implementing. Thank you very much.
Very excellent information can be found on website.
I dugg some of you post as I thought they were extremely helpful invaluable
Hello, you used to write excellent, but the last few posts have been kinda boring… I miss your tremendous writings. Past few posts are just a little bit out of track! come on!
I’m not sure where you are getting your information, but good topic. I needs to spend some time learning more or understanding more. Thanks for wonderful info I was looking for this info for my mission.
Wohh precisely what I was searching for, thanks for posting.
I’ve recently started a website, the info you provide on this site has helped me greatly. Thanks for all of your time & work.
Just a smiling visitant here to share the love (:, btw great design and style .
I like your writing style truly loving this web site. “You simply *must* stop taking advice from other people.” by Melissa Timberman.
I like this website because so much useful stuff on here : D.
We wish to thank you again for the stunning ideas you gave Jeremy when preparing a post-graduate research in addition to, most importantly, pertaining to providing all of the ideas within a blog post. If we had known of your website a year ago, i’d have been rescued from the unwanted measures we were employing. Thanks to you.
Interesting. I never knew there was such crap on the net.
A person essentially help to make critically posts I would state. That is the very first time I frequented your web page and up to now? I amazed with the research you made to make this particular publish amazing. Magnificent activity!
I believe you have noted some very interesting points , thankyou for the post.
I simply could not leave your web site before suggesting that I extremely loved the standard information a person supply for your guests? Is going to be back steadily in order to check out new posts.
Its wonderful as your other articles : D, regards for posting . “As experience widens, one begins to see how much upon a level all human things are.” by Joseph Farrell.
i truly apreciate this post and hope you continue to supply us with interesting information. cheers
My spouse and i still can’t quite think that I could always be one of those studying the important ideas found on your web site. My family and I are sincerely thankful for the generosity and for giving me the advantage pursue the chosen profession path. Thank you for the important information I got from your web-site.
Hey! awesome blog! I happen to be a daily visitor to your site (somewhat more like addict 😛 ) of this website. Just wanted to say I appreciate your blogs and am looking forward for more to come!
I appreciate an unbelievable publish, may examine your others posts. many thanks for your thinking within this, I felt a lttle bit made an impact to by this article. Many thanks again! You earn a terrific moment. Portrays natures best by the wonderful report here. I do think that if a greater number of people consideration for it like this, they’d have got a better time period receive the suspend ofing the matter.
I believe other website proprietors should take this site as an model, very clean and fantastic user friendly style . “He who lives without folly isn’t so wise as he thinks.” by Francois de La Rochefoucauld.
Thank you for the blog post. Jones and I are already saving for our new guide on this issue and your blog post has made us to save money. Your thinking really answered all our inquiries. In fact, in excess of what we had acknowledged prior to when we stumbled on your fantastic blog. I actually no longer have doubts and a troubled mind because you completely attended to the needs in this article. Thanks
Hey There. I discovered your weblog working with msn. This can be a very neatly written write-up. I will make sure to bookmark it and come back to learn added of one’s valuable information. Thank you for the post. I will undoubtedly return.
I do agree with all the ideas you have presented in your post. They’re very convincing and will definitely work. Still, the posts are too short for beginners. Could you please extend them a little from next time? Thanks for the post.
Your place is valueble for me. Thanks!…
Hiya! Fantastic blog! I happen to be a daily visitor to your site (somewhat more like addict 😛 ) of this website. Just wanted to say I appreciate your blogs and am looking forward for more!
I’m having a small problem. I’m unable to subscribe to your rss feed for some reason. I’m using google reader by the way.
I merely wanted to thank you yet again for your amazing website you have built here. It’s full of ideas for those who are definitely interested in this particular subject, particularly this very post. You’re really all amazingly sweet and also thoughtful of others as well as reading your site posts is a great delight to me. And thats a generous present! Jeff and I really have enjoyment making use of your tips in what we should do in a few days. Our checklist is a mile long and tips will definitely be put to great use.
Very interesting topic, appreciate it for posting.
Youre so cool! I dont suppose Ive read anything like this before. So good to find someone with some unique ideas on this subject. realy thank you for starting this up. this web site is one thing that is wanted on the web, somebody with just a little originality. helpful job for bringing one thing new to the web!
I like this site very much so much superb information.
I wanted to follow up and allow you to know how great I cherished discovering your blog today. I would consider it a honor to operate at my business office and be able to use the tips discussed on your website and also participate in visitors’ opinions like this. Should a position regarding guest writer become offered at your end, please let me know.
Aw, this was an extremely nice post. Spending some time and actual effort to create a top notch article… but what can I say… I hesitate a lot and never seem to get nearly anything done.
Wow, I have a blog too but I can’t write as well as you do. Good stuff.
How to integrate PAYPAL in ASP.NET webpages ?
Give me one example on this concept
plz help me
Nice article..
But I have found another simple example to call a WCF RestFul Service using jQuery AJAX Call, please refer to link below:-
http://www.etechpulse.com/2014/03/consume-restful-wcf-service-in-aspnet.html
http://www.etechpulse.com/2014/03/how-to-consume-wcf-restful-service-in.html
Thanks