HOW TO: Create and Test an XML Web Service in Visual Basic .NET (309013)
The information in this article applies to:
- Microsoft .NET Framework 1.0
- Microsoft .NET Framework 1.1
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual Basic .NET (2003)
- 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- 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.
- In Solution Explorer, change the name of Service1.asmx to
Services.asmx.
- Open Services.asmx in the visual designer. In the
Properties window, change the Name property of the Service1 class to Services.
- 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:
<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
- 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.
- 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.
- Fill in any requested message parameter
values.
- Click Invoke.
- View the resulting XML and close the results
window.
- Click the Back button to return to the method list and repeat the steps for the
remaining Web methods.
- Close the built-in browser.
back to the top
Create the Test Client Application- On the File menu, click Add Project, and then click New Project.
- Select Visual Basic Console Application, and then name the project TestHarness.
- On the Project menu, click Add Web Reference.
- In the Address field, type
http://localhost/TestService/Services.asmx, and then
click Go.
- Click Add Reference to finish creating the Web reference.
- 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.
- 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 - Create a breakpoint on the following line:
Console.WriteLine(myService.GetMessage)
- In Solution Explorer, right-click the TestHarness project, and then click Set as StartUp Project.
- On the Debug menu, click Start and wait for the program to enter debug mode.
- 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.
- On the Debug toolbar, use Step Into to step through each line of code from the TestHarness client
into the XML Web service.
- Before you end the Main subroutine, confirm that the output in the console window is as
expected.
- 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
Modification Type: | Major | Last Reviewed: | 1/21/2004 |
---|
Keywords: | kbHOWTOmaster KB309013 kbAudDeveloper |
---|
|