Creating a Webservice and Consuming the Webservice Using a Web Application

The following are the definitions of Web Services.

Definition 1:

A Web Service is a software application, accessible over the Web through an URL that is accessed by clients using XML-based protocols, such as Simple Object Access Protocol (SOAP) sent over accepted Internet protocols, such as HTTP. Clients access a Web Service application through its interfaces and bindings, that are defined using XML artifacts, such as a Web Service Definition Language (WSDL) file.

Definition 2:

Web Services are typically Application Programming Interfaces (API) or web APIs that can be accessed over a network, such as the internet, and executed on a remote system hosting the requested services.

Definition 3:

A Web Service can be defined as a service available through the World Wide Web (internet or intranet) that uses XML as the message system and is not tied to any programming language or Operating System (OS).

Some people have doubts about where this Web Service concept is relevant.

The best example is, you want to buy a Kingfisher flight ticket online. You can book it from the Kingfisher website. Likewise you can also book that ticket from the makemytrip.com website. Here makemytrip.com is using the Kingfisher Web Service.

Now I will explain about my Web Service.

Note: All files are available in the download file.

Here I am explaining Web Services (how to create a Web Service and how to consume that Web Service using a web application) in an understandable way.

The following is the procedure to create a Web Service and publish it.

  1. Create a New website and create a virtual directory in IIS.
  2. Add an asmx file.
  3. In the .cs file write your functionality.
  4. Run the Asmx file.

The following is the procedure to consume a Web Service.

  1. Create a New website and create a virtual directory in IIS
  2. Create an aspx file and add a web reference.
  3. Invoke the properties of the Web Service.
  4. Run the application.

I created a new website called webparts and created a virtual directory in IIS with that same name.

Now I created the asmx application. When we create the file .cs file also create the .asmx file. The .cs file will be in the App_Code folder. In the .cs file I wrote the Web Service functionality Addition, Subtract, Multiplication and Hello methods.

This is the code of the App_Code/ FirstWebService.cs file:

 

[WebService(Namespace = "http://localhost/webparts")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class FirstWebService : System.Web.Services.WebService

{

    public FirstWebService () {

 

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

      [WebMethod]

      public int Add(int a, int b)

      {

          return a + b;

      }

    [WebMethod]

    public int Sub(int a, int b)

    {

        return a - b;

    }

    [WebMethod]

    public int Multiplication(int a, int b)

    {

        return a * b;

    }

  [WebMethod]

  public String Hello()

  {

      return "Welcome to the Web Service";

  }   

}

Now run the FirstWebService.asmx file.

p1.png

Click on Service Description.

p2.png

Click on methods like Add and so on.

p3.png

Finally in a Visual Studio command prompt run the following commands.

WSDL http://local/webparts/FirstWebService.asmx?WSDL
csc /t: library FirstWebService.cs

FirstwebserviceCommand.gif

When we run these commands, the next FirstWebService.cs and FirstWebService.dll files will be created like this in your C or D drive.

Firstwebservicecsndll.gif

Now I create the new website. In this website I created an aspx file.

Add the Web Reference like this.

1. Right-click on the website and click on add Web Reference. This screen will be shown.

FirstwebserviceConsumer.gif

Click on the Web Services in the local machine. It will show all the Web Services that exists in your system. Click on FirstWebservice.asmx and click Add Reference.

FirstwebserviceConsumerClick.gif

The Web Reference is added to your site (the disco, disomap and wsdl files will be created under the App_Referances folder).

2. Add FirstWebService.cs and FirstWebService.dll to the bin folder (that were created in the C or D drive).

The Webservconsumer.aspx file code behind code is:

Imports System

Imports System.Data

Imports System.Configuration

Imports System.Collections

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.HtmlControls

Imports System.Web.Services

 

Partial Class webservconsumer

    Inherits System.Web.UI.Page

 

    Public Sub runSrviceAdd_Click(ByVal sender As [Object], ByVal e As EventArgs)

        Dim mySvc As New FirstWebService()

        Label1.Text = mySvc.Hello()

        Label2.Text = mySvc.Add(Int32.Parse(txtNum1.Text), Int32.Parse(txtNum2.Text)).ToString()

    End Sub

 

    Protected Sub runSrviceSub_Click(ByVal sender As ObjectByVal e As System.EventArgs) HandlesrunSrviceSub.Click

        Dim mysvcsub As New FirstWebService()

        Label1.Text = mysvcsub.Hello()

        Label2.Text = mysvcsub.Sub(Int32.Parse(txtNum1.Text), Int32.Parse(txtNum2.Text)).ToString()

    End Sub

 

    Protected Sub runSrviceMulti_Click(ByVal sender As ObjectByVal e As System.EventArgs)Handles runSrviceMulti.Click

        Dim mysvcMulti As New FirstWebService()

        Label1.Text = mysvcMulti.Hello()

        Label2.Text = mysvcMulti.Multiplication(Int32.Parse(txtNum1.Text), Int32.Parse(txtNum2.Text)).ToString()

    End Sub

End Class 

Run the Webservconsumer.aspx file. The output screen will be like this.

FirstwebserviceWebservice.gif
If you have any doubts regarding this then you can leave me a message.

Up Next
    Ebook Download
    View all
    Learn
    View all