Microsoft KB Archive/301273

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

Article ID: 301273

Article Last Modified on 3/29/2007

-

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 Q301273





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

IN THIS TASK
SUMMARY
 * Requirements
 * Write a Simple .asmx Web Service
 * Consume a Web Service

REFERENCES



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

back to the top

Requirements
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

back to the top

Write a simple .asmx Web service
 Start Visual Studio .NET or Visual Studio 2005. 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. Change the name of the Solution file to MathService for consistency. Change the name of the default Web service that is created from Service1.asmx to MathService.asmx . 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 .  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 </li> Click Build on the Build menu to build the Web service.</li> 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.</li></ol>

back to the top

Consume a Web service
<ol> Start Visual Studio .NET or Visual Studio 2005.</li> Create a new Console Application project.</li> 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). <ol style="list-style-type: lower-alpha;"> On the Project menu, click Add Web Reference.</li> 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.</li> Click Add Reference.</li> Expand the Web References section of Solution Explorer and note the namespace that was used.</li></ol> </li>  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 </li>  Invoke a method on the proxy object created in the previous step: Console.Write(&quot;2 + 4 = {0}&quot;, myMathService.Add(2,4)) </li> Close and save the project.</li></ol>

back to the top

<div class="references_section">