Microsoft KB Archive/309013

= HOW TO: Create and Test an XML Web Service in Visual Basic .NET =

Article ID: 309013

Article Last Modified on 1/21/2004

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework Class Libraries 1.1

-



This article was previously published under Q309013





IN THIS TASK
SUMMARY
 * Requirements
 * Create a New XML Web Service Application
 * Create the XML Web Service Methods
 * Test the Services with Visual Studio .NET
 * Create the Test Client Application
 * Create the Test Code
 * Test the Client Application

REFERENCES



SUMMARY
This step-by-step article describes how to create and test an XML Web service created using ASP.NET by using Visual Studio .NET, and how to test the XML Web service with a simple Microsoft Visual Basic .NET console client.

XML Web services are quickly becoming the preferred way to communicate with applications outside of the domain of the application. XML Web services enable clients to access component functionality across the Internet without configuring firewalls to allow access to internal components.

back to the top

Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need:
 * Development with Visual Basic .NET
 * Developing and using components

back to the top

Create a New XML Web Service Application

 * 1) On your local computer (localhost), start Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual Basic Projects, then click ASP.NET Web Service under Templates. Name the project TestService.
 * 2) In Solution Explorer, change the name of Service1.asmx to Services.asmx.
 * 3) Open Services.asmx in the visual designer. In the Properties window, change the Name property of the Service1 class to Services.
 * 4) Save the project.

back to the top

Create the XML Web Service Methods
 Open Services.asmx in the code editor.  Add the following code within the Services class definition to create various Web methods:  Public Function GetMessage As String Return &quot;Today is the day&quot; End Function

 _ Public Function SendMessage(ByVal message As String) As String Return &quot;Message received as: &quot; & message End Function

 _ Public Function ReverseMessageFunction(ByVal message As String) As String Return StrReverse(message) End Function

 Public Sub ReverseMessageSub(ByRef message As String) message = StrReverse(message) End Sub  Save and build the project.

back to the top

Test the Services with Visual Studio .NET
 In Solution Explorer, right-click Services.asmx and then click View in Browser.</li> Follow these steps to use the built-in browser to test each Web method:NOTE: You cannot test the ReverseMessageSub procedure because it expects a ByRef argument.

 Click the hyperlink for the method that you want to test.</li> Fill in any requested message parameter values.</li> Click Invoke.</li> View the resulting XML and close the results window.</li> Click the Back button to return to the method list and repeat the steps for the remaining Web methods.</li></ol> </li> Close the built-in browser.</li></ol>

back to the top

Create the Test Client Application

 * 1) On the File menu, click Add Project, and then click New Project.
 * 2) Select Visual Basic Console Application, and then name the project TestHarness.
 * 3) On the Project menu, click Add Web Reference.
 * 4) In the Address field, type http://localhost/TestService/Services.asmx, and then click Go.
 * 5) Click Add Reference to finish creating the Web reference.
 * 6) In Solution Explorer, right-click localhost in the Web References folder, click Rename, and then change the name to WebService. This becomes the namespace that is used within the test application to refer to the Services class.

back to the top

Create the Test Code
 Open Module1.vb and locate the Sub Main procedure.</li>  Paste the following code in the file to call the appropriate Web methods: Dim strValue As String = &quot;This is my message&quot; Dim myService As New WebService.Services Console.WriteLine(myService.GetMessage) Console.WriteLine(myService.SendMessage(strValue)) Console.WriteLine(myService.ReverseMessageFunction(strValue)) myService.ReverseMessageSub(strValue) Console.WriteLine(strValue) </li></ol>

back to the top

Test the Client Application
  Create a breakpoint on the following line: Console.WriteLine(myService.GetMessage) </li> In Solution Explorer, right-click the TestHarness project, and then click Set as StartUp Project.</li> <li>On the Debug menu, click Start and wait for the program to enter debug mode.</li> <li>On the Debug menu, click Windows, and then click Locals. Use the Locals window to view the value of the strValue variable during the debugging to observe any changes that are made to the variable.</li> <li>On the Debug toolbar, use Step Into to step through each line of code from the TestHarness client into the XML Web service.</li> <li>Before you end the Main subroutine, confirm that the output in the console window is as expected.</li> <li>When the program ends, remove the breakpoint and close Visual Studio .NET.</li></ol>

back to the top

<div class="references_section">