How to Create ASP.NET web Service and how to consume it: A Beginner’s Guide
Creating this post especially for my friend. Who want to learn How to create His First Web Service. If you too want to do the same then go ahead and start your visual studio.
Service
-
Click on Web Site Project
-
It will open New Web site Dialog box. Choose ASP.NET web service and give it appropriate name(HelloWorldService) and select your Coding language(C#). Click OK.
It will take some time because it will make everything ready for you sir. So, you can start quickly
-
What it has created?
Service.cs: is the main file where you can write you code. E.g application logic etc..Calling Data Access layer etc.
Service.asmx will expose your service to outer world.
-
Web Method
If you will see Service.cs it has one method already written. You can use it as a reference implementation
[WebMethod]
public
string HelloWorld() {
return
“Hello World”;
}
Any method you want to expose you have to declare it by attribute WebMethod
Let’S create our new method.So, you can get more into methods.
Just copy paste above one and modify it will like this:
///
<summary>
/// This method will take argument as a string and show it with greeting
///
</summary>
///
<param name=”name”></param>
///
<returns></returns>
[WebMethod]
public
string
MyHelloWorld(string name)
{
return
“Hello World,”+name;
//Here you can call your Insert,Update,Delete from DAL and write return statement
}
- Build Web Site [CTRL+SHIFT+B]. That’s it we have our service Ready. Let’s create one consumer for it.
Consumer
Our Service is ready. It will take name and return it with Hello World name. I am creating here one Windows application for demo. You can create any client to use it.[That’s why it is service 🙂 ]
1. Click on your solution file name Means [Your focus should be on C:\HelloService or your solution name]
2. Select File | Add | New Project
3. It will open Add New Project Dialog Box. Select Windows application and give appropriate name to it. Click OK
4. So now you will have structure as under:
5. Right Click on HelloWorldConsumer project and Click on Add Web Reference- For Adding service reference.
6.It will open Add Web Reference window. Just click on Web Services in this solution. Because we have our service under same solution.
7. It will find out service. Just click on it.
8. It will load our methods as shown here: [Web reference name I had changed “localhost” to “HelloWorldClient”. So you should also do it. Its good practice]. Click on Add reference button. – Which will add our service reference to client.
9. Now your soln structure should look like:
10. Click on Form1 and drag some controls so we can test it. So, your form should look like this.
11. Double click on “Call Service” Button it is Buttton1 for me. For creating its Click event handler code. And it will point it to handler code call your service here and show result in Messagebox as shown here.
12. Build solution. And ensure that HelloWorldConsumer-windows application is your startup project. Then run the project [CTRL+F5]
13. looks Cool… It’s working man. Try with your name and enjoy!!!
Links:
http://quickstarts.asp.net/QuickStartv20/webservices/
http://www.w3schools.com/webservices/default.asp
Happy Service Coding!!!
I have also kept solution here for reference. But I request you first try yourself and if get some error then and then try this. Pls try to do it yourself first. you can download it from Here
Update:
Add Reference in VS 2008 — http://www.carlosfemmer.com/post/2008/01/How-to-add-web-reference-in-VS-2008-Project.aspx
Thanks to Ravindra Joshi for pointing it out!
Thanks,
Its a valuable guidance for a beginner to enter into the Web Services concept good work keep it up.
Regards,
Mohanraj
Really good stuff. I am definitely benifit by your work, Thanks and keep posting some good stuff.
regards,
Raju
Hey thanks a lot…
Implementation has been explained in such a structured manner.
Simply the best.
Regards,
Thanks Purnima/All,
Thanks for nice words!!it really stimulates me and boosts me up for writing more and more good stuff..:)
Reagrds,
Kiran
Kiran,
Its good start up tutorial for beginners.. Can you also post similar one for explaining service reference (applicable for framework 3.0), it would be helpful..
Regards,
Balaji
Yes Balaji Sir,
I guess you mean WCF For Begineers…yeap, really good idea..will do it as i get bit time..:)
Thanks for visiting my blog!
-Kiran
По моему Вам нужно чаще отдыхать, очень уж Вы заработались.
Что-то такое слышал, но не так подробно, а откуда материал брали?
Только вчера об этом думал, так что пост как нельзя в тему!
Really cool article Kiran. Searched almost eight to nine sites but with no luck. But this straightaway gave the solution.
Only one small correction, rather updation
“Add a Web Reference” option will not be present when we right-click on the project. We need to follow the instructions present at following link
Thanks,
Ravindra.
Hello Ravindra,
Thanks for correction. Post updated.
Cheers,
Kiran
Giving the link mentioned in my previous comment. Sorry for SPAM.
http://www.carlosfemmer.com/post/2008/01/How-to-add-web-reference-in-VS-2008-Project.aspx
Thanks,
Ravindra.
Thank you very much for this article..I got a clear idea of web service
Hi kiranpatils,
This is a very good article. Very helpful for beginners. I searched through the net and found some pages which weren’t much useful for me and at last found yours. It’s very helpful for me since I’m a beginner. Now I’m trying with web application and trying to add new features. Thanks for such a wonderful article.
Regards,
Daniel.
Very Nice great work thankx
Regards
Zaid Bin Zafar
Hi Kiran, It’s really good article for beginners, in a very simple language with each step explained.
Is there any article explained by you for “Browse UDDI Servers on the local network” kind of web service? Kindly let me direct towards it if any.
Thanks.
Kinz — Glad to know that you found it useful! Are you still looking for any help?
Thanks for your comment — It stimulates!
Keep sharing, Keep visiting and Keep reading! 🙂
Sincerely,
Kiran Patil
Hi ,
Really very good article to start web service . I tried it but I am getting error while calling service as below :
No overload for method ‘HelloWorld’ takes 1 arguments
Please guide for the same .
Thank You
Vedangi — Glad to know that you found it useful! Apologize for delay in reply, you are still looking for a help or you solved it?
Thanks for your comment — It stimulates!
Keep sharing, Keep visiting and Keep reading! 🙂
Sincerely,
Kiran Patil
Amazing and so simple to understand. Thanks !
Shreepad — Glad to know that you found it useful!
Thanks for your comment — It stimulates!
Keep sharing, Keep visiting and Keep reading! 🙂
Sincerely,
Kiran Patil
thanks for help 🙂
Dolly – Glad to know that you found it useful!
Thanks for your comment — It stimulates!
Keep sharing, Keep visiting and Keep reading! 🙂
Sincerely,
Kiran Patil
Its really help full, but i have problem when i am using this with Oracle database following 3 tyre arch. first i created one class file where i implemented logic which execute at database end, next in service.asmx.cs file i call the database connection then i run the Oracle command and returns dt (data table)I have writen the logic in “public datatable mylogic(string natid). at last i have run the service which redirect the service index page where i select myservice (mylogic) to enter string value(natid) and submit (invoke), after while i am getting the error (HTTP 500). i also debug the application in which i find that result which returning datatable.
Please Guide me …….
Hello Mohammed,
It seems you got some error at your service level. Have you tried to debug your service?
Is it possible for you to share a sample code with me?
Sincerely
Kiran Patil
Hi,
Today web service are used widely in web application. Provided information by you, are very informative. Thanks for sharing your knowledge. There are few other
article/blog (related to web service) that also useful for developers.
http://mindstick.com/Articles/8b01f39d-841d-4ec4-a393-507e692cd080/?Web%20Service%20in%20NET
http://msdn.microsoft.com/en-us/library/ms972326.aspx
http://publib.boulder.ibm.com/infocenter/cmx/v8r3m0/index.jsp?topic=%2Fcom.ibm.cmx.doc%2Fcmbapmst231.htm
Thank you for a very clear direct beginning tutorial on web services. So many people seem to get lost in all sorts of peripheral information before they finally get to the point of what they’re trying to teach. It was refreshing to be able to simply sit down and get right to the information that I was looking for.
nice work.
Take a look into this http://www.etechpulse.com/2012/09/all-about-web-service-aspnet.html