Thursday, 5 January 2012

C#.Net How To: Create a Web Service in C#.net Visual Studio 2010

I have observed that most of the beginners don't know how to create a web service. Hence i thought to write an article on web services.

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.
Click here to see video: How to create asp.net web service

Create a web service in c#.net

  1. Open a new project window. To open a new project window Click on File -> New -> Project.. menu.
    C# - Create a new Web Application
    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 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
        {

            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }

            [WebMethod]
            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 –
    C# - Web Service Output
    C# -Web Service Output

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

In this article you have learn to create a web service 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.

Protected by Copyscape Plagiarism Check

Sharing is Caring...





26 comments:

  1. Really a very simple and helpful tutorial.

    ReplyDelete
  2. Really a very simple and helpful tutorial.

    ReplyDelete
  3. Hi, can I use a Visual Studio express for this ? Thx

    ReplyDelete
    Replies
    1. Yes Margo, you can use express edition...

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

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

    ReplyDelete
    Replies
    1. 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.

      Delete
  6. This tutorial seems to be simple and very useful. Thanks

    ReplyDelete
  7. very beautifully defined Thanks a lot...

    ReplyDelete
  8. 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

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

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

    ReplyDelete
    Replies
    1. 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. :)

      Delete
  10. 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..?

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

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

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

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

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

    ReplyDelete
  16. How are you Brad Pitt? Best hello world ever :) made me LOL

    ReplyDelete
  17. Thank you very much, was worth my time

    ReplyDelete