C#.Net How To: Create a Webservice in C#.net & Visual Studio 2010

How to create a webservice in c#.net is a very simple task. But i have observed that most of the beginners don’t know how to create a web service in C#.net. Hence i thought to write an article on web services. First, let’s learn what is webservice?

What is webservice?

The general idea about webservice is that Web service is a mechanism that provides data as a service/response over the http protocol on request of some other programs. Webservice is nothing but a software function that helps us to communicate to some other programs over the network using http. To communicate and exchange the data between two software system, web service uses XML.

This c# tutorial explains how to create a Web Service in c# in Visual Studio 2010 with very easy and simple steps. Once web service is created you can publish web service in IIS and then consume web service in visual studio 2010.

Watch Video: How to create asp.net web service

Steps to create a webservice in c#.net

  1. Open a new project window to create a webservice in c#.net. To open a new project window Click on File -> New -> Project.. menu.
    create a webservice in C#
    C# – Create a new Web Application
  2. Change the .Net framework to 3.5. Select ASP.NET Web Service Application to create a new Web Service Application.
    C# - Select new Web Application
    C# – Select new Web Application

    Give here Name to the Web Service Application. I have used MyFirstWebServiceApplication as a WebService name.

  3. Click on Ok button to create Web Service application. As soon as you click on Ok button, you will get auto generated code with default web method “HelloWorld”. The class name will be “Service1” by default. You can add your own web methods to this class.
    C# - Default HelloWord Code
    C# – Default HelloWord Code
  4. Now, I am going to add one Web Method called “MyFirstWebMethod” that takes two input string. The code will appears as –
        public class Service1 : System.Web.Services.WebService
            public string HelloWorld()
                return “Hello World”;
            public String MyFirstWebMethod(String firstName, String lastName)
                //return “How are you ” + firstName + ” ” + lastName + “?”;
                return String.Format(“How are you {0} {1}?”, firstName, lastName);
  5. Execute the code by hitting F5 button on keyboard. The resulting page will appear in your default browser.
    C# -Web Service List of Methods
    C# -Web Service List of Methods
  6. Click on MyFirstWebMethod. It will prompt you to enter two parameters.
    C# -Web Service Method Parameter
    C# -Web Service Method Parameter

    Enter here firstname and lastname strings and click on Invoke.

  7. The result will appear as –
    create a webservice in c# - output
    Create a webservice in c# – output

Click here to watch video: How to create asp.net web service

In this article you have learn to create a webservice in c#. I hope this articles will meet your expectations. If you find this article helpful, then could you please share the article on your social network?

Following articles may help you to consume and publish the web services.

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

30 Comments+ Add Comment

  • Really a very simple and helpful tutorial.

  • Really a very simple and helpful tutorial.

  • Hi, can I use a Visual Studio express for this ? Thx

  • good blog :) I also write articles about .NET developing

  • why are you choose .Net framework to 3.5?
    Can I choose .Net framework to 4.0?

    • Hi there, how r u? Thanks for commenting.
      Yes, you can always choose 4.0 instead of 3.5. I was bit comfortable with 3.5 hence while writing the article i chose 3.5. And of course there are few difference between the two, however it would not affect if you choose 4.0 particularly for this article.

  • This tutorial seems to be simple and very useful. Thanks

  • very beautifully defined Thanks a lot…

  • Thanks so much for your article. I will like to ask question. I can use db direcly when i created webservice. For example, I have username and password. [WebMethod]
    public String wl_app_ident(String Username, String Password)
    return String.Format(Username, Password);
    Every 60 days, i have to change password, it is expired. I will like to use db,i don’t want upgrade my code every 60 days just change password. I hope you can give advice. Thanks you so much

    • Thanks for comments Chama. However, your question is not clear to me. Could you please elaborate more?

  • good stuff. keep it up.

  • m not getting the web service option after selecting .net framework 4.0

    • Microsoft is completely focussing on WCF service over ASP.Net web service. Hence, they have kept it little hidden on “New Project” creation templates. WCF is actually much more faster than ASP.net web service and can do everything that ASP.net web service does. However, you can always create a ASP.net web service in 4.0.
      1) Create a new “ASP.NET Web Application”
      2) Add new item – “Web service”.
      It would solve your problem, please let me know in case not. :)

  • hey,this is the nice tutorial for simple web-service,but can you please tell me about something a web-service which shows the authentication process.
    means a web-service which authenticate users from database..?

  • Thankyou so much for this effort of yours.
    God bless you.

  • thank you so much for this….
    can u tell me also how to create window service.??

  • Thank you for this post.
    really nice and very easy to understand

  • Thankyou for article..this is really very helpful for beginners like me!

  • Thank you Ashish Ramteke for your interesting Tutorial…It is helpful l and Simple for all interested person. Please keep it up

  • How are you Brad Pitt? Best hello world ever :) made me LOL

  • Thank you very much, was worth my time

  • Thank you so much bro.. its really help full.. have a good day

  • It does not get easier than this. Thanks for the whole set. And for splitting it up into manageable parts.

  • EXCELLENT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Leave a comment