Click here to Skip to main content
15,886,806 members
Articles / Web Development / ASP.NET

ASP.NET Web Services – Getting Started

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
21 Jul 2014CPOL4 min read 9.2K   7   3
How to get started with ASP.NET Web services

In the past 10 months, we were discussing about ASP.NET MVC, Entity Framework, jQuery, LINQ and a lot more stuff. Today, we are going to start with ASP.NET Web Services.

What are Web Services and Why Should We Use Them?

Web services are a standardized way for developing interoperable applications. That is, enabling an application to invoke a method of another application. These applications can be on the same computer or on different computers. Web services use open standards and protocols like HTTP, XML and SOAP. Since these are open and well known protocols, applications which are built on any platform can interoperate with web services. For example, a JAVA application can interoperate with a web service which is built using .NET. Similarly, a web service which is built using JAVA can be consumed by a .NET application.

Hyper Text Transfer Protocol (HTTP) is the protocol which is widely used by web services to send and receive messages. The messaging protocol is SOAP. SOAP stands for Simple Object Access Protocol. SOAP messages are in XML format.

Let’s try to understand these with an example. To create a sample ASP.NET web service, follow the below steps.

Step 1

Create an ASP.NET Empty Web Application and name it as WebServicesDemo.

Step 2

Right click on WebServicesDemo project in solution explorer and add New Item. From the Add New Item dialog box, select Web Service. Change the name from WebService1.asmx to CalculatorWebServices.asmx.

Asp.NetWebServices1

Asp.NetWebServices2

Notice that WebService is a class that is decorated with [WebService] attribute and inherits from System.Web.Services.WebService base class. The [WebService] attribute tells that this class contains the code for a Web Service. WebService Namespace is used to uniquely identify your web service on the internet from other services that are already there on the web. WebService Namespace can be any string, but it is common to give a company’s internet domain name as they are usually unique. For example, [WebService(Namespace = "http:// BestTEchnologyblog.com/webservices")]

To allow a web service to be called from JavaScript using ASP.NET AJAX, decorate the WebService class with [System.Web.Script.Services.ScriptService] attribute.

Step 3

Copy and paste the following code in CalculatorWebServices.asmx.

using System.Web.Services;

C#
namespace WebServicesDemo
{
    [WebService(Namespace = "http://BestTEchnologyblog.com/webservices")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class CalculatorWebServices : System.Web.Services.WebService
    {
        [WebMethod]
        public int Add(int firstNumber, int secondNumber)
        {
            return firstNumber + secondNumber;
        }
    }
}

Asp.NetWebServices3

Notice that the CalculatorWebServices class contains a single method called Add(). This method adds 2 numbers and returns the sum. This method is decorated with [WebMethod] attribute. If you want this method to be exposed as part of the web service, the method must be public and should be decorated with [WebMethod] attribute. This attribute has got several properties which can be used to configure the behavior of the XML web service method. We will discuss these properties in the later articles.

At this point, we have successfully created a Web Service. Build the solution and run the application. We should get a page like below:

Asp.NetWebServices4

We can see a link called Service Description on the upper right hand side of the page. If we click on the link, it takes us to WSDL page. WSDL stands for Web Service Description Language.

Asp.NetWebServices5

On the other hand, when we click on the Add method, it will take us to a page where we can test our web service method. Let’s pass firstNumber as 10 and secondNumber as 20 and click on the Invoke button.

Asp.NetWebServices6

We will get a result of 30.

Asp.NetWebServices7

So points here to ponder are the following:

  • WebService is a class that is decorated with [WebService] attribute and inherits from System.Web.Services.WebService base class. The [WebService] attribute tells that this class contains the code for a web service.
  • WebService Namespace is used to uniquely identify your web service on the internet from other services that are already there on the web. WebService Namespace is common to be given a company’s internet domain name as they are usually unique.
  • To allow a web service to be called from JavaScript using ASP.NET AJAX, decorate the WebService class with [System.Web.Script.Services.ScriptService] attribute.
  • If you want a method to be exposed as part of the web service, the method must be public and should be decorated with [WebMethod] attribute.

What’s Next?

In the next article, we will discuss about Consuming a web Service from a client application.

Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at http://BestTEchnologyBlog.com. Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

Comments and Discussions

 
GeneralMy vote of 3 Pin
jaguar8423-Jul-14 6:24
professionaljaguar8423-Jul-14 6:24 
QuestionWhy ? Why whould I use this and not the new , modern WEB API approach ? Pin
Member 800306322-Jul-14 0:37
Member 800306322-Jul-14 0:37 
AnswerRe: Why ? Why whould I use this and not the new , modern WEB API approach ? Pin
jaguar8423-Jul-14 6:23
professionaljaguar8423-Jul-14 6:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.