Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

ASP.Net Web Services – Getting Started

, 21 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
CodeProject In the past 10 months, we were discussing about ASP.Net MVC, Entity Framework, jQuery, LINQ and many more stuffs. 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 app

In the past 10 months, we were discussing about ASP.Net MVC, Entity Framework, jQuery, LINQ and many more stuffs. 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 a 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;

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 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)

Share

About the Author

Arun Ramachandran India
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 Pinmemberrichard_x8623-Jul-14 7:24 
QuestionWhy ? Why whould I use this and not the new , modern WEB API approach ? PinmemberMember 800306322-Jul-14 1:37 
AnswerRe: Why ? Why whould I use this and not the new , modern WEB API approach ? Pinmemberrichard_x8623-Jul-14 7:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 21 Jul 2014
Article Copyright 2014 by Arun Ramachandran India
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid