Microsoft KB Archive/309013

From BetaArchive Wiki

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


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

  1. Open Services.asmx in the code editor.
  2. Add the following code within the Services class definition to create various Web methods:

      
    <WebMethod()> Public Function GetMessage() As String
        Return "Today is the day"
    End Function
    
    <WebMethod()> _
    Public Function SendMessage(ByVal message As String) As String
        Return "Message received as: " & message
    End Function
    
    <WebMethod()> _
    Public Function ReverseMessageFunction(ByVal message As String) As String
        Return StrReverse(message)
    End Function
    
    <WebMethod()> Public Sub ReverseMessageSub(ByRef message As String)
        message = StrReverse(message)
    End Sub
                        
  3. Save and build the project.

back to the top

Test the Services with Visual Studio .NET

  1. In Solution Explorer, right-click Services.asmx and then click View in Browser.
  2. 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.

    1. Click the hyperlink for the method that you want to test.
    2. Fill in any requested message parameter values.
    3. Click Invoke.
    4. View the resulting XML and close the results window.
    5. Click the Back button to return to the method list and repeat the steps for the remaining Web methods.
  3. Close the built-in browser.

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

  1. Open Module1.vb and locate the Sub Main procedure.
  2. Paste the following code in the file to call the appropriate Web methods:

      
    Dim strValue As String = "This is my message"
    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)
                        

back to the top

Test the Client Application

  1. Create a breakpoint on the following line:

    Console.WriteLine(myService.GetMessage)
                        
  2. In Solution Explorer, right-click the TestHarness project, and then click Set as StartUp Project.
  3. On the Debug menu, click Start and wait for the program to enter debug mode.
  4. 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.
  5. On the Debug toolbar, use Step Into to step through each line of code from the TestHarness client into the XML Web service.
  6. Before you end the Main subroutine, confirm that the output in the console window is as expected.
  7. When the program ends, remove the breakpoint and close Visual Studio .NET.

back to the top

REFERENCES

For more information about creating and testing XML Web services that use ASP.NET, see the "Creating XML Web Services in Managed Code" and "XML Web Services Created Using ASP.NET and XML Web Service Clients" topics in the reference documentation for Visual Studio .NET and in the .NET Framework documentation.

back to the top

Keywords: kbhowtomaster KB309013