XML Web services are reusable units of application logic that you
can expose to clients across the Internet. Web services are
platform-independent. In addition, Web services are based on standards that the
industry agrees upon such as Extensible Markup Language (XML), Simple Object
Access Protocol (SOAP), and Hypertext Transfer Protocol (HTTP). Client
applications can be any of the following:
Web-based ASP.NET application
Windows application
Pocket PC application
Mobile device application
Console application
XML Web services provide a new form of connectivity across your
entire enterprise. Visual Studio .NET or Visual Studio 2005 makes it easy to create and to use XML
Web services.
Build a Web service
In this section, you create an XML Web service that implements
the Pythagorean theorem.
Create a new ASP.NET Web service in Visual Basic .NET or in Visual Basic 2005 as
follows:
Start Visual Studio .NET or Visual Studio 2005, and then click New Project.
Under Project Types, click Visual Basic Projects. Under Templates, click ASP.NET Web Service.
Note In Visual Studio 2005, click Visual Basic under Project Types.
In the Name text box, type
PythagoreanTheoremWS.
Switch to the Code window for Service1.asmx. To do this,
right-click Service1.asmx in Solution Explorer, and then click View Code.
Add the following code before the End Class statement to create a new function:
Public Function PythagoreanTheorem(ByVal a As Double, _
ByVal b As Double) As Double
End Function
The Pythagorean theorem states that the square of the
hypotenuse of a right triangle is equal to the sum of the squares of the other
two sides. Add the following code inside the PythagoreanTheorem function to implement this mathematical formula:
Dim dblSum As Double
dblSum = a ^ 2 + b ^ 2
Return Math.Sqrt(dblSum)
This function will fully implement the Pythagorean theorem.
However, the function is not yet a Web service method. To expose a function as
a Web service method, add the WebMethod attribute to the method declaration. The complete function should
appear as follows:
<WebMethod()> _
Public Function PythagoreanTheorem(ByVal a As Double, _
ByVal b As Double) As Double
Dim dblSum As Double
dblSum = a ^ 2 + b ^ 2
Return Math.Sqrt(dblSum)
End Function
On the Build menu, click Build Solution to compile this Web service.
Use the Web service
In this section, you create a Windows application that uses this
Web service.
Create a new Console Application project in Visual Basic
.NET or in Visual Basic 2005 to test the Web service that you created in the previous
section.
To access a Web service from a client application, the
client must first include a reference to the Web service. To add a Web
reference, open the Solution Explorer window, right-click the project, and then
click Add Web Reference.
In the Add Web Reference dialog box, click Web References on Local
Server. Visual Studio .NET or Visual Studio 2005 searches the local computer for any
available Web service. This may take a few moments.
In the Available References section, click http://localhost/PythagoreanTheorem/PythagoreanTheorem.vsdisco, and then click Add Reference.
Switch to the Code window for Module1.vb, and then add the
following code to the Sub Main procedure:
Dim hypotenuse As Double
Dim ws As New localhost.Service1()
'Pythagorean Triple: 3, 4, 5
hypotenuse = ws.PythagoreanTheorem(3, 4)
Console.WriteLine(hypotenuse)
'Pythagorean Triple: 5, 12, 13
hypotenuse = ws.PythagoreanTheorem(5, 12)
Console.WriteLine(hypotenuse)
'Pythagorean Triple: 7, 24, 25
hypotenuse = ws.PythagoreanTheorem(7, 24)
Console.WriteLine(hypotenuse)
Console.Read()
On the Debug menu, click Start to run the application. The Console window displays the following
output:
Imports System.Web.Services
<WebService(Namespace := "http://tempuri.org/")> _
Public Class Service1
Inherits System.Web.Services.WebService
#Region " Web Services Designer Generated Code "
Public Sub New()
MyBase.New()
'The Web Services Designer requires this call.
InitializeComponent()
'Add your own initialization code after the InitializeComponent() call.
End Sub
'The Web Services Designer requires this code.
Private components As System.ComponentModel.IContainer
'NOTE: The Web Services Designer requires this procedure.
'You can use the Web Services Designer to modify the procedure.
'However, do not use the Code editor to modify it.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
'CODEGEN: The Web Services Designer requires this procedure.
'Do not use the Code editor to modify it.
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#End Region
' WEB SERVICE EXAMPLE
' The HelloWorld() sample Web service returns the string Hello World.
' To build, uncomment the following lines, and then save and build the project.
' To test this Web service, ensure that the .asmx file is the start page,
' and then press F5.
'
'<WebMethod()> Public Function HelloWorld() As String
' HelloWorld = "Hello World"
' End Function
<WebMethod()> _
Public Function PythagoreanTheorem(ByVal a As Double, ByVal b As Double) As Double
Dim dblSum As Double
dblSum = a ^ 2 + b ^ 2
Return Math.Sqrt(dblSum)
End Function
End Class
Module Module1
Sub Main()
Dim hypotenuse As Double
Dim ws As New localhost.Service1()
'Pythagorean Triple: 3, 4, 5
hypotenuse = ws.PythagoreanTheorem(3, 4)
Console.WriteLine(hypotenuse)
'Pythagorean Triple: 5, 12, 13
hypotenuse = ws.PythagoreanTheorem(5, 12)
Console.WriteLine(hypotenuse)
'Pythagorean Triple: 7, 24, 25
hypotenuse = ws.PythagoreanTheorem(7, 24)
Console.WriteLine(hypotenuse)
Console.Read()
End Sub
End Module