Concept of Web service in VB.NET

To understand the concept of web service we will see this artical.
  • 6287

Web service:

 

Web services are for specific job on internet. It is not fully based on internet.It is capable to use IIS as host. These services are use to perform work remotely on net. Web services are the platform independent.

 

Web service is simply an application that expose a web accessible API. That means you can invoke this application programmatically over the web. Web services allow application to share data using SOAP (simple object access protocol). SOAP is message based protocol on request and response.

 

Credit card validation we can expose as a web service (this is the best example of web service).

 

There are three attributes for the platform independent and all web services follows these attributes.

 

  • HTTP
  • SOAP
  • XML 

SOAP=HTML+XML.

 

Html is a stateless protocol that is use for communication. We use XML for data transfer because all application understand XML format.

 

Web service can invoke single method on a stateless object. When we add reference of any class library then a dll copy in the debug folder. If we do local property false then we can use that .dll with full namespace.

 

.vsdisco will contain the reference of web method of web service.

 

.asmx is the web service extension in .net. There is no user interface of web service.
 

We can keep our business logic in web service.

 

Where can we find web service?

 

Web services are registered in the UDDI (universal discovery description and integration) directory. So web service can be found from www.uddi.org.

 

Steps for creating the web service:

 

Step 1: we open the web service in Microsoft visual studio2005. And by default we find a Web method. We can create another method.

 
Imports System
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace = "http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)> _ 
Public Class Service
    Inherits System.Web.Services.WebService
    Public Sub New()
        'Uncomment the following line if using designed components 
        'InitializeComponent(); 
    End Sub
    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function
End Class 

Step2: we build and debug service and we get that service as follows: 

 

  Image6.gif

 

Step3: After that copy the URL of the service.

 

For Example: http://localhost:1096/service1/Service.asmx?op=sum.

 

Step4: Now we open the asp.net web application.

 

Step5: We add the reference as add web reference.Then new page will be open.

 

Step6: Paste the URL and click on go then we found service after that click on add reference.

 

Step7: AT the last we writen the code of the application. and debug that application. 

 

Example: Write a program for sum of two integers with the help of web service.

 

Open the asp.net web service and create a WebMethd as Sum().

 

Service.vb

 
Imports System
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace = "http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)> _ 
Public Class Service
    Inherits System.Web.Services.WebService
    Public Sub New()
        'Uncomment the following line if using designed components 
        'InitializeComponent(); 
    End Sub
    <WebMethod()> _
    Public Function sum(ByVal a As IntegerByVal b As IntegerAs Integer
        Return a + b
    End Function
End Class 

Build and debug this code and copy the URL. Now open the asp.net application.

 

 ImageSum.gif

 

Webform.aspx:

 

<%@ Page Language="VB" AutoEventWireup="true"  CodeFile="Default.aspx.vb" Inherits="_Default"%>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div><table cellpadding="0" cellspacing="0" border="2" width="50%" height="250px">

    <tr><td align="center" valign="top" style="padding-top:20px;">

        <asp:Label ID="Label1" runat="server" Text="Enter the first no."></asp:Label>

        &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br><br><br>

        <asp:Label ID="Label2" runat="server" Text="Enter the second no"></asp:Label>

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br><br><br>

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

        <asp:Button ID="Button1" runat="server" Text="Sum" OnClick="Button1_Click" /><br><br><br>

        &nbsp;<asp:Label ID="Label3" runat="server" Text="Result"></asp:Label>

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;

        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

    </td></tr></table></div>

</form>

</body>

</html> 

 

 Image1.gif


To add web reference:

We add the reference as add web reference. And new page will be open as follows:

Image4.gif

Paste the URL and click on go then we found service : 

 Image5.gif

After that click on add reference button then reference will be add.

 

Web form.aspx.vb:

 

Now we add the reference as add web reference. And write the code on the button click as follows


Imports
 System
Imports System.Data
Imports System.Configuration
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

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Dim obj As localhost.Service = New localhost.Service()
    Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs)
        Dim a As Integer, b As Integer, c As Integer
        a = Int32.Parse(TextBox1.Text)
        b = Int32.Parse(TextBox2.Text)
        'c = obj.sum(a, b);
        TextBox3.Text = (obj.sum(a, b)).ToString()
    End Sub
End Class 

Now we debug the web application and we found the following output: 

 

  Image2.gif

 

Properties of Webmethod:

 

A web method attributes represents a method for web. Web methods have six properties. These are as follows:

 

  • Description
  • Message name
  • Enable session
  • Cache duration
  • Transaction option
  • Buffer response 

Description:

 

[Web service] and [Web method] attribute have a description property.

 
Imports System
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace = "http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)> _ 
Public Class Service
    Inherits System.Web.Services.WebService
    Public Sub New()
        'Uncomment the following line if using designed components 
        'InitializeComponent(); 
    End Sub
    <WebMethod(Description = "this method will add")> _
    Public Function sum(ByVal a As IntegerByVal b As IntegerByVal c As IntegerAs Integer
        Return a + b + c
    End Function
End Class 

Message name:

 

This is useful property, when we want to overload the web method. 

 
<WebMethod()> _
Public Function add(ByVal a As IntegerByVal b As IntegerByVal c As IntegerAs Integer
    Return a + b + c
End Function

(WebMethod (MessageName="add")) 'message

Public Function add(ByVal a As IntegerAs Integer
    Dim s As Integer = 0
    Dim i As Integer
    For i = 0 To a Step i + 1
        s = s + i
    Next
    Return s
End Function 

Enable session:

 

This property is used for to enable in the web services. (Remember web services used http protocol this is stateless). if we want to maintain the state between client and server we need to use this property. Default enable session is false.

 
<WebMethod(EnableSession = True)> _
Public Function HelloWorld() As String
    Return "Hello World"
End Function 
 

Cache duration:

 

When we cache the previously accessed result next time the same user or different user asks we can serve from cache. Here result cached 60 milliseconds. If we invoke this method within sixty minutes, it will print same time. This will increase the web service performance

 
<WebMethod(CacheDuration = 60)> _
Public Function showtime() As String
    Return DateTime.Now.ToShortTimeString()
End Function   

Buffer response:

 

This property is Boolean control web method whether or not to buffer the method's response.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.