This tutorial gives an introduction into Web Services and will show how we can create a self-contained Web Service with three simple Web Methods. C# version.

This basic tutorial will serve as an introduction to Web Services. In it, we will see how we can create a simple Web Service with multiple methods.
Similar to ASP.NET web applications, Web Services can be created in Visual Studio.NET in two ways – either in one file, with the header and class code in the same document, or a code-behind file can be utilized. This tutorial will use a code-behind.

Web Services can essentially be created in notepad, but Visual Studio makes it so much easier, that it hides unnecessary and repetitive code from the developer. Usually, a Web Service is consumed by a web application or windows form and the Web Methods are called remotely. For demonstration purposes, in this tutorial we will create a self-contained Web Service. This means that when we compile and run it, we will have direct access to the Web Methods.

The first thing we do is start a new Web Site in Visual Studio, and choose ASP.NET Web Service. We are presented with a .ASMX file with the following:

Visual Studio will also create the web.config for us, and will look something like this:

To create a self-contained Web Service, all we need to do is write the methods in the code-behind. When we compile and run it, Visual Studio will provide its default Web Service page, displaying the methods. From this page, we can test the web methods by clicking on each one. If the web method requires paraments, these will be in the form of textboxes, which we will be able to fill in and submit.
The three web methods we are going to write will be very simple: One will be the Hello World, with a parameter for a name. This will display “Helo World, #name”, where #name is replaced by the user-submitted parameter. The second web method will return the result of two numbers being added together. The two numbers will be supplied by the user via two textboxes. The third will simply return the server time.

The web methods will look something like this:

Download Source Files