Practical Approach To ASP.NET Web Services - Part One - Overview

Introduction

A Web Service may be defined as an independent and self-sustained unit of a software application that may be hosted on the internet. Suppose, you could design specific functionality of a web service to execute the business logic of an application?

A Web service can do many things. It also can be used to implement various functionalities, like calculating interest, calculating pay slips and so on. You can create a Web Service in ASP.NET 4.5 and above, which is known as ASP.NET 4.5 Web Service and consists of an infrastructure that contains various components used to implement basic functionality, such as calling a Web Service, locating a Web Service and so on. You can also create Web Service that uses the ASP.NET AJAX functionality.

So, what are Web Services?

In simple words, web service is an application or software that is programmed to perform a functionality to interact over the internet or over the networks. Web services are language independent as they communicate using standard protocols, like HTTP, SOAP and XML.

NOTEASMX Web services are a legacy technology. Most of the companies are now using WCF (Windows Communication Foundation) to build XML Web Services and XML Web Service client. In this series, we will cover ASP.NET Web service and how to use ASP.NET Web Services.

Web services are a standardized way for developing interoperable applications; i.e., enabling an application to invoke a method of another application. These applications can be used on the same computer or different computers as well.

A web service allows the user to interact with other applications or websites irrespective of the programming language in which they are developed or created. Web service can be accessed by any application, regardless of the hardware and software platform in which it is running, because web services use protocols like SOAP (Simple Object Access Protocol) and WSDL(Web Service Description Language ). Web services use open standards and protocols, like HTTP, XML, and SOAP. Since these are well known protocols, the application built on any platform can inter-operate with web service.

Example

A JAVA application can inter-operate with a web service built using .NET. Similarly, a web service built in JAVA can be consumed by a .NET application .

A web service contains all logic which can be bundled into one application and that application can interact with multiple applications at the same time. They can be shared with multiple applications without a need to install them on an  individual Server or Client machine.

Architecture of Web Service

Web service

A web service architecture consists of an application (be it Console application, Web application and so on) as ASP.NET web service application and other ASP.NET Web service files, like .asmx and .dll files. You need a web Server to monitor both your application and web services file, and a database in order to save or retrieve the data.

Now, we will see how to build a web service.
 
Let's flip to Visual Studio. Here, I am using VS 2015.
 
Go to File -> New Project.

Web service

Click on Web and give a suitable name.

Web service

Create an "Empty" application.

Web service

Now, in that project, we will add a web service.
 
Right click on Solution, go to Add -> New Item. 

Web service

Give your web service name as "Calculator.asmx".

Web service

When you click on "Add", the  Calculator Service will get created. Now, let's examine the auto-generated code.

Web service

As you can see, the Calculator we had added is a class, but this class service is declared with web service attributes. Basically, this attribute tells that this class Calculator contains web service code.

The web service has got namespace property. Basically, these namespaces are used to uniquely identify your web service from the other web services that are already there on the web-bed.

Example

Here, we have this calculator web service. When we are done building this, we will publish this on the internet. Now, another developer develops a same service name as calculator. So basically, to uniquely identify your web service from the other web services that are already there on the web, we use this namespace property which actually is a string.
 
When you see the intelliSense, you can set it to any string but it is common for a namespace to be like a company name or a domain name, in order for being uniquely identified.

Web service

Here, I have mentioned c-sharpcorner.com. This will help us in uniquely identifying the service. 

Now, if you look properly at the class calculator, it is actually inheriting from another class as System.Web.Services.Webservice.
 
Is it mandatory to inherit the web service from this class?
 
Not really. But, if the ASP.NET has to use some session like application state objects, then this web service should have direct access to those objects. Thus, we make it inherited from this web service class.

Now, if you look at this web service, it has got one method "Helloworld" that is declared with WebMethod attribute.
 
Basically, if you want to expose a method as the part of the web service that requests to a client to a calling application, then that method needs to be declared with a WebMethod attribute. If you don’t declare with WebMethod attribute, the client application will not be able to see that method.
 
So, if you want a method to be exposed to a client, then that method needs to be declared with a WebMethod attribute and that method needs to be Public as well .

Now, this attribute WebMethod has several properties.

Web service

These are used to customize the behavior of your web service. We will see this in details in our upcoming articles. Now, let’s change the return type of this method from "string" to "int", and pass two parameters as first number and second number.

Web service

Alright ! We have a working web service. Let's build that web service. Once it is successfully built, run the service by pressing "ctrl + F5".

Web service

We are running this service on Google Chrome. You will see that we don’t see the HTTP protocol. Now, copy the URL and paste it in a notepad. You will see that we are actually using the HTTP protocol.

Web service

In order to access the web applications, we use HTTP protocol. HTTP (Hyper Text Transfer protocol) is the protocol widely used by Web services to send and receive messages. Web services also use other protocols like SMTP (Simple Mail Transfer protocol ) but the most widely used is HTTP.

Now, here, I have a Service Description link. We will click on that.

Web service

Now, look at the URL above.

Web service

It's WSDL ( Web Service Description Language ). We will discuss it in our upcoming parts where we will discuss how to consume this web service from a client application, like an ASP.NET web application or a Windows application .

Generally, this is used by the clients to generate a proxy class, which we will see in detail in our upcoming articles.

Now, in our web service, if we remove WebMethod attribute:

Web service

As you can see, we don’t see our Add Method now. Now, un-comment that and run your web service again.
 
Click on Add method. We see the parameters as FirstNumber and SecondNumber.

Web service

Now, let's invoke the parameters and see what output we get.

Web service

As you can see, we got an output as 20. Now, apart from the interface to test the web service method on this page, we also have sample request and response SOAP messages.

Web service

As you can see, it's in XML format but it is formatted to a SOAP protocol. So, self specification states that you know there needs to be SOAP envelope and SOAP Body.

Web service

Since we are not using SOAP Headers, it's not displayed here. But, there is SOAP body here and it contains firstnumber and secondnumber, the parameters for the Add Method. If you look at response message, we get an integer back, which is nothing but the sum of two numbers. Again, it has SOAP Envelope and SOAP Body.

Web service

This is how SOAP response and request method looks. Now, if you see carefully, there are two SOAP Versions 1.1 and 1.2. Soap Version 1.2 is the latter version, so obviously, there are several changes in these two versions.
 
 That's it. In the next article, you will get to learn the following: 
 
Practical Approach to ASP.NET Web Services - Part Two- Consuming a Web Service.