Skip to content
April 23, 2008 / kiranpatils

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

  1. Click on Web Site Project

  2. 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

  3. 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.

  4. 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

}

  1. 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!

 

31 Comments

Leave a Comment
  1. Mohanraj / Sep 4 2008 2:53 am

    Thanks,

    Its a valuable guidance for a beginner to enter into the Web Services concept good work keep it up.

    Regards,
    Mohanraj

  2. Raju / Dec 29 2008 11:23 am

    Really good stuff. I am definitely benifit by your work, Thanks and keep posting some good stuff.

    regards,
    Raju

  3. Purnima / May 10 2009 3:02 pm

    Hey thanks a lot…
    Implementation has been explained in such a structured manner.
    Simply the best.

    Regards,

    • kiranpatils / May 11 2009 4:30 am

      Thanks Purnima/All,

      Thanks for nice words!!it really stimulates me and boosts me up for writing more and more good stuff..:)

      Reagrds,
      Kiran

  4. Balaji G / May 21 2009 10:32 am

    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

    • kiranpatils / May 21 2009 11:45 am

      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

  5. Cederash / May 23 2009 9:48 pm

    По моему Вам нужно чаще отдыхать, очень уж Вы заработались.

  6. Ferinannnd / May 24 2009 6:54 pm

    Что-то такое слышал, но не так подробно, а откуда материал брали?

  7. Avertedd / May 26 2009 8:08 am

    Только вчера об этом думал, так что пост как нельзя в тему!

  8. Ravindra Joshi / Oct 25 2009 3:36 pm

    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.

    • kiranpatils / Nov 1 2009 5:48 pm

      Hello Ravindra,

      Thanks for correction. Post updated.

      Cheers,
      Kiran

  9. Ravindra Joshi / Oct 25 2009 3:39 pm

    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.

  10. Raggu / Oct 4 2010 9:28 pm

    Thank you very much for this article..I got a clear idea of web service

  11. Daniel / Jun 30 2011 11:43 am

    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.

  12. Zaid Bin Zafar / Jan 9 2013 6:36 am

    Very Nice great work thankx
    Regards
    Zaid Bin Zafar

  13. Kinz / Feb 4 2013 8:10 am

    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.

    • kiranpatils / Mar 23 2013 7:07 pm

      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

  14. Vedangi / Feb 7 2013 2:08 am

    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

    • kiranpatils / Mar 23 2013 7:06 pm

      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

  15. Shreepad / Feb 11 2013 12:29 pm

    Amazing and so simple to understand. Thanks !

    • kiranpatils / Mar 23 2013 7:05 pm

      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

  16. Dolly / Feb 26 2013 7:46 am

    thanks for help🙂

    • kiranpatils / Mar 23 2013 7:04 pm

      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

  17. Mohammed Ahammed / May 1 2013 6:01 am

    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 …….

    • kiranpatils / May 1 2013 6:12 pm

      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

  18. Jayden Bellus / May 29 2013 2:35 pm

    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

  19. David / Oct 29 2013 1:14 am

    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.

  20. etechpulse / May 1 2014 7:18 am

    nice work.
    Take a look into this http://www.etechpulse.com/2012/09/all-about-web-service-aspnet.html

Trackbacks

  1. Website
  2. Daily programming techniques for all developers.
  3. Blue Ray Plus - Latest Technology News

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: