Microsoft KB Archive/315935

= How to build and use XML Web services by using Visual Studio .NET or Visual Studio 2005 =

Article ID: 315935

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q315935



IN THIS TASK
SUMMARY Requirements XML Web Services
 * Build a Web Service
 * Use the Web Service

Complete Code Listing REFERENCES



SUMMARY
This article demonstrates how to create and use an XML Web service with Visual Studio .NET or Visual Studio 2005.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
 * Microsoft Internet Information Server 4.0 or Microsoft Internet Information Services 5.0

This article assumes that you are familiar with the following topics:
 * Visual Basic .NET or Visual Basic 2005
 * Visual Studio .NET or Visual Studio 2005

back to the top

XML Web services
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.

back to the top

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 </li>  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) </li>  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 </li> On the Build menu, click Build Solution to compile this Web service.</li></ol>

back to the top

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.</li> 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.</li> 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.</li> In the Available References section, click http://localhost/PythagoreanTheorem/PythagoreanTheorem.vsdisco, and then click Add Reference.</li>  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 </li> On the Debug menu, click Start to run the application. The Console window displays the following output:

<pre class="fixed_text">5 13 25

</li></ol>

back to the top

Complete code listing
Imports System.Web.Services

<WebService(Namespace := &quot;http://tempuri.org/&quot;)> _ Public Class Service1 Inherits System.Web.Services.WebService


 * 1) Region &quot; Web Services Designer Generated Code &quot;

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


 * 1) 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 = &quot;Hello World&quot; ' 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 back to the top

<div class="references_section">