Web Services in ASP.NET: Part 1

A web service is a web application that is basically a class consisting of methods that could be used by other applications on the “http://” protocol. It also follows a code-behind architecture like the ASP.NET web pages, although it does not have a user interface.

It's an alternate option of C# Remoting Architecture, in Remoting we can communicate from a server to a client on tcp:// channel but there should be .NET framework installed on both ends, on the server and also on the client side because the server returns an object in Plane Old CLR Object (POCO) format that is only understandable by CLR.

A web service class can be a collection of web methods with return types that can serializable directly as XML or JSON.

Create a simple WebService Application in Visual Studio using the following procedure.

Step 1: Open Visual Studio then go to File -> New -> Web Site.

create new website

Step 2: Select HTTP Web Location and write a name for "ASP.NET Empty Web Site".

empty website

Step 3: Go To Solution Explorer and right-click the project then select Add New Time -> Web Service.

add web service

Step 4: Write a name for the Web Service file and then it will add a file for the web site with “.asmx “ (Active Server Method Extension) extension and also add a code file (.cs/.vb) .

type service name

Then add a web Service .

web service

Step 5: Delete the .cs File from App_Code and add a new class file to create a Code Behind file from .asmx file to host a web service class.

add class

specify name

Step 6:

Use “using System.Web.Services; “ first to use the [WebService] and [WebMethod] attributes.

[WebService]: This attribute defines the class that we need to host by “.asmx” file and a client-side proxy class is necessary to create the same structure of this class.

[WebMethod]: This attribute defines the method that we can call from the client side by the URL of my WebService class and it also serializes the return value of the method in the format of XML or JSON.

web service class

Step 7: Finally edit your “.asmx “ file To define the the object of the WebService class with a property like:

  1. Language: Language Property can be define by C#/VB in Which language we are going to create my CodeBehind file .cs/.vb.

  2. CodeBehind: The CodeBehind property can be defined by the address of that .cs/.vb file that we have hosted in the App_Code folder.

  3. Class: Class is a property that we need to define by the name of that class that exists in the Code Behind file and declared by the attribute [WebService].

    code behind

Step 8: Now build your WebService application and right-click on “.asmx” and view in the browser.

vew in browser

Then the following output will be in the browser.

say Hello

There will be a list of all the methods declared by the attributes [WebMethod] in the [webService] class. We can test all the [WebMethod] attributes on the local system where my Web Service Application is hosted by the click on the method name.


Pass all the formal parameter values into the TextBox and click to test the return value of this method in the format of XML by default. If you want to return JSON data we will learn about that in the next article.

Format of XML