How to write a simple Web service by using Visual Basic .NET or Visual Basic 2005

For a Microsoft Visual C# .NET version of this article, see
308359 .



This article describes how to write a simple Web service, called MathService, that exposes methods for adding, subtracting, dividing, and multiplying two numbers.


The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that are required:
  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, Microsoft Windows Server 2003, or Windows NT 4.0 Server
  • Microsoft Internet Information Server 4.0 or Internet Information Services 5.0 or later
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
This article assumes that you are familiar with the following topics:
  • How to use the Visual Studio .NET or Visual Studio 2005 integrated development environment

Write a simple .asmx Web service

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. Create a new Active Server Pages (ASP) .NET Web service project. Name the Web service MathService and point the location to an appropriate Web server that is running ASP.NET if necessary.
  3. Change the name of the Solution file to
    MathService for consistency.
  4. Change the name of the default Web service that is created from Service1.asmx to MathService.asmx.
  5. Click Click here to switch to code view in the designer environment to switch to code view.

    Change the name of the class from Public Class Service1 to Public Class MathService.
  6. Define methods that encapsulate the functionality of your service. Each method that will be exposed from the service must be flagged with a WebMethod attribute in front of it. Without this attribute, the method will not be exposed from the service.

    NOTE: Not every method needs to have the WebMethod attribute. It is useful to hide some implementation details called by public Web service methods or for the case in which the WebService class is also used in local applications. A local application can use any public class, but only WebMethod methods will be remotely accessible as Web services.

    Add the following method to the MathServices class that you just created:
    <WebMethod()> Public Function Add(a As Integer, b As Integer) As Integer
    Return(a + b)
    End Function

    <WebMethod()> Public Function Subtract(A As System.Single, B As System.Single) As System.Single
    Return A - B
    End Function

    <WebMethod()> Public Function Multiply(A As System.Single, B As System.Single) As System.Single
    Return A * B
    End Function

    <WebMethod()> Public Function Divide(A As System.Single, B As System.Single) As System.Single
    If B = 0
    Return -1
    End If
    Return Convert.ToSingle(A / B)
    End Function
  7. Click Build on the Build menu to build the Web service.
  8. Browse to the MathService.asmx Web service page to test the Web service. If you set the local computer to host the page, the URL is http://localhost/MathService/MathService.asmx.

    The ASP.NET runtime returns a Web Service Help Page that describes the Web service. This page also enables you to test different Web service methods.

Consume a Web service

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. Create a new Console Application project.
  3. Add a reference for the MathService Web Service to the new console application.

    This step creates a proxy class on the client computer. After the proxy class exists, you can create objects based on the class. Each method call that is made with the object then goes out to the uniform resource identifier (URI) of the Web service (usually as a SOAP request).
    1. On the Project menu, click Add Web Reference.
    2. In the Add Web Reference dialog box, type the URL for the Web service in the Address text box and press ENTER. If you set the local computer to host the Web service, the URL is http://localhost/MathService/MathService.asmx.
    3. Click Add Reference.
    4. Expand the Web References section of Solution Explorer and note the namespace that was used.
  4. Create an instance of the proxy object that was created. Place this code in the Main procedure of the Module1 module:
    Dim myMathService As localhost.MathService = New localhost.MathService()
  5. Invoke a method on the proxy object created in the previous step:
    Console.Write("2 + 4 = {0}", myMathService.Add(2,4))
  6. Close and save the project.


Programming the Web with Web Services (Visual Studio .NET Help)

ASP.NET Web Services and ASP.NET Web Service Clients (Microsoft .NET Framework Developer's Guide)

Extreme XML: UDDI: An XML Web Service (MSDN Voices column): Web Services Description Language Tool (Wsdl.exe) (Microsoft .NET Framework Tools)

DHTML Dude: Accessing Web Services From DHTML (MSDN Voices column): For more information, see the following Microsoft Training & Certification course: For more information, see the following book:
Balena, Francesco. Programming Microsoft Visual Basic .NET (Core Reference). Microsoft Press, 2002.

ID do Artigo: 301273 - Última Revisão: 19 de jun de 2009 - Revisão: 1