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

Article translations Article translations
Article ID: 315935 - View products that this article applies to.
This article was previously published under Q315935
Expand all | Collapse all

On This Page

SUMMARY

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

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

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.

Build a Web service

In this section, you create an XML Web service that implements the Pythagorean theorem.
  1. Create a new ASP.NET Web service in Visual Basic .NET or in Visual Basic 2005 as follows:
    1. Start Visual Studio .NET or Visual Studio 2005, and then click New Project.
    2. 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.
    3. In the Name text box, type PythagoreanTheoremWS.
  2. Switch to the Code window for Service1.asmx. To do this, right-click Service1.asmx in Solution Explorer, and then click View Code.
  3. 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
    					
  4. 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)
    					
  5. 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
    					
  6. 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.
  1. 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.
  2. 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.
  3. 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.
  4. In the Available References section, click http://localhost/PythagoreanTheorem/PythagoreanTheorem.vsdisco, and then click Add Reference.
  5. 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()
    					
  6. On the Debug menu, click Start to run the application. The Console window displays the following output:
    5
    13
    25
    						

Complete code listing

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
				

REFERENCES

For more information, visit the following Microsoft Web sites:
What are XML Web Services?
http://msdn.microsoft.com/en-us/library/aa286548.aspx

XML Web Services Basics
http://msdn.microsoft.com/en-us/library/ms996507.aspx

Creating and Accessing XML Web Services Walkthroughs
http://msdn.microsoft.com/en-us/library/cbsxhx87(VS.80).aspx

Properties

Article ID: 315935 - Last Review: December 6, 2006 - Revision: 5.5
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbhowtomaster KB315935

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com