ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
XML and Web Services Unleashed
XML and Web Services Unleashed

Find Prices
Sample Chapter
Sample Chapter
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

Back To Main
[How to:] Create and Consume XML Web Service
9th April, 2003

AUTHOR
Ken, Lin Kam Hung
Microsoft MVP,
Microsoft(Taiwan) Community Star
MCP, MCP+I, MCSA, MCSE(NT4 & Win2K), MCSE+I,
MCDBA(SQL7 & SQL2K), MCSD(VB6), MCAD(VB.NET), 
Vice President of Hong Kong .NET User Group

SUMMARY
An XML Web Service is an internet standard and this article demonstrates how to build and consume an XML Web Service in Visual Basic .NET.

REQUIREMENTS
The following list outlines the recommended hardware, software, network infrastructure, and service pack that you need: 
-- Visual Basic .NET
This article assumes that you are familiar with the following topic: 
-- Visual Basic .NET

OBJECTIVES
After Completing this lab, you will be able to :
1) Create an XML Web Service that providing a simple calculation;    Exercise 1
2) Create an Web Application that consumes the XML Web Service;    Exercise 2

EXERCISE 1 CREATING AN XML WEB SERVICE

In this exercise, you will create an XML Web Service project that you will in later exercises. You will create few Web Service Methods that doing simple calculation.

Create an XML Web Service Solution
1) Open Visual Studio .NET, Click New Project;
2) Select ASP.NET Web Service Template from Visual Basic Project Project Type;
3) Type http://localhost/WSCalculation in the location;
4) Rename Service1.asmx into SimpleCalculation.asmx in the Solution Explorer.

Create XML Web Service Methods
1) Right click on SimpleCalculation.asmx and click View Code;
2) Change the service1 into SimpleCalculation, your code should be look like,

    Public Class SimpleCalculation
       
Inherits System.Web.Services.WebService

3) Insert the Add Function with the following code,

    <WebMethod()> _
    Public Function Add(ByVal x as Integer, ByVal y as Integer) As Integer
        Return (x + y)
    End Function

4) Insert the Subtract, Multiply and Divide functions;
5) Your code should look like as the following,

    Imports System.Web.Services

    <System.Web.Services.WebService(Namespace :="http://tempuri.org/WSCalculation/SimpleCalculation")> _
    Public Class SimpleCalculation
        Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function Add(ByVal x as Integer, ByVal y as Integer) As Integer
        Return (x + y)
    End Function

    <WebMethod()> _
    Public Function Subtract(ByVal x as Integer, ByVal y as Integer) As Integer
        Return (x - y)
    End Function

    <WebMethod()> _
    Public Function Multiply(ByVal x as Integer, ByVal y as Integer) As Integer
        Return (x - y)
    End Function

    <WebMethod()> _
    Public Function Divide(ByVal x as Integer, ByVal y as Integer) As Integer
        Return (x / y)
    End Function

    End Class

Debug and Build the XML Web Service
1) Click Start from the Debug Menu;
2) An Internet Explorer should be open if there is no error in your project;
3) You may test the web methods' functionalities by clicking any link;
4) Close Your Internet Explorer and leave your Visual Studio .NET and the solution open

EXERCISE 2 USING THE XML WEB SERVICE

In this Exercise, You will create an Web Application project that use the Web Service which built in last exercise

Create the Web Application
1) In Solution Explorer, right click your solution and click Add-->New Project(if you have close Visual Studio .NET, you may open it again and select on WSCalculation);
2) Select ASP.NET Web Application Template from Visual Basic Project  Project Type;
3) Type http://localhost/SimpleMath in the Location;
4) Open WebForm1.aspx from the Solution Explorer;
5) Drop two TextBox, one Label, one Button, one DropDownList and two RequiredFieldValidator to the Design Panel;
6) Right click on WebForm1.aspx and click View Code to open the WebForm1.aspx.vb
7) Add the following code in Page_Load Section to set the inital Setting,

    If Not Page.IsPostBack Then
        Button1.Text = " = "
        Me.DropDownList1.Items.Add(New ListItem("+", "Add"))
        Me.DropDownList1.Items.Add(New ListItem("-", "Subtract"))
        Me.DropDownList1.Items.Add(New ListItem("*", "Multiply"))
        Me.DropDownList1.Items.Add(New ListItem("/", "Divide"))
        Label1.Text = ""
        Me.RequiredFieldValidator1.ControlToValidate = "Textbox1"
        Me.RequiredFieldValidator1.Display = ValidatorDisplay.Dynamic
        Me.RequiredFieldValidator1.ErrorMessage = "Please enter value"
        Me.RequiredFieldValidator2.ControlToValidate = "textbox2"
        Me.RequiredFieldValidator2.Display = ValidatorDisplay.Dynamic
        Me.RequiredFieldValidator2.ErrorMessage = "Please enter value"
    End If

8) In Solution Expplorer, right click Reference under the current project and select Add Web Reference;
9) Type http://localhost/WSCalcuation/SimpleCalculation.asmx in the URL and click Add Reference;
10) You will see a new Web Reference has added and rename the Localhost into WSCalculation;
11) Open the WebForm1.aspx and double click on Button1, a new function called Button1_Click has been added to WebForm1.aspx.vb;
12) Add the following code under Button1_Click,

    Dim WS1 As New WSCalculation.SimpleCalculation
    Dim x, y, z As Integer

    x = CType(TextBox1.Text, Integer)
    y = CType(TextBox2.Text, Integer)
   
    Select Case Me.DropDownList1.SelectedValue
        Case "Add"
            z = WS1.Add(x, y)
        Case "Subtract"
            z = WS1.Subtract(x, y)
        Case "Multiply"
            z = WS1.Multiply(x, y)
        Case "Divide"
            z = WS1.Divide(x, y)
    End Select 

    Label1.Text = z

13) Now your code should be look like as the following,

    Public Class WebForm1
        Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load
        If Not Page.IsPostBack Then
            Button1.Text = " = " 
            Me.DropDownList1.Items.Add(New ListItem("+", "Add"))
            Me.DropDownList1.Items.Add(New ListItem("-", "Subtract"))
            Me.DropDownList1.Items.Add(New ListItem("*", "Multiply"))
            Me.DropDownList1.Items.Add(New ListItem("/", "Divide"))
            Label1.Text = ""
            Me.RequiredFieldValidator1.ControlToValidate = "Textbox1"
            Me.RequiredFieldValidator1.Display = ValidatorDisplay.Dynamic
            Me.RequiredFieldValidator1.ErrorMessage = "Please enter value"
            Me.RequiredFieldValidator2.ControlToValidate = "textbox2"
            Me.RequiredFieldValidator2.Display = ValidatorDisplay.Dynamic
            Me.RequiredFieldValidator2.ErrorMessage = "Please enter value"
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim WS1 As New WSCalculation.SimpleCalculation
        Dim x, y, z As Integer

        x = CType(TextBox1.Text, Integer)
        y = CType(TextBox2.Text, Integer)
   
        Select Case Me.DropDownList1.SelectedValue
            Case "Add"
                z = WS1.Add(x, y)
            Case "Subtract"
                z = WS1.Subtract(x, y)
            Case "Multiply"
                z = WS1.Multiply(x, y)
            Case "Divide"
                z = WS1.Divide(x, y)
        End Select 

        Label1.Text = z
    End Sub
End Class

Debug and Build the Web Application that Consuming XML Web Service
1) In Solution Explorer, right click on SimpleMath Project and select Set As Startup Project;
2) Right click on WebForm1.aspx and Select Set As Start Page;
3) Click Start from the Debug Menu;
4) An Internet Explorer should be open if there is no error in your project;
5) You may test the application by entering some value to the TextBoxes and click the Button;
6) Close your Internet Explorer, Close Visual Studio .NET.

back to top

 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/8/2009 1:41:04 AM