How to access a Web service in a Windows-based application by using Visual Basic 2005 or Visual Basic .NET


This step-by-step article describes how to access a Web service in a Microsoft Windows-based application by using Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET.

Create a Web service

  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. On the Filemenu, point to New, and then click

    Note In Visual Studio 2005, click Web Site instead of Project.
  3. Under Project Types, click to select Visual Basic Projects.

    Note In Visual Studio 2005, omit this step.
  4. Under Templates, click to select
    ASP.NET Web Service.
  5. In the Location box, type
    http://WebServerName/TestWebService, and then click OK.

    Note WebServerName is a placeholder for the name of the Web server.
  6. In Solution Explorer, right-click Service1.asmx, and then click View Code.
  7. In the Code view, uncomment the default HelloWorld() Web Service method, as follows:
    <WebMethod()> Public Function HelloWorld() As String
    HelloWorld = "Hello World"
    End Function
    Note If you uncomment the WebMethod() attribute for a public method, the method is exposed as part of the XML Web service.
  8. On the Build menu, click Build TestWebService.

Create a Windows-based application

Add the Web service reference to the Windows-based application

You can add a reference to a Web service by using Visual Studio, or you can generate the proxy for the Web service by using the Wsdl.exe tool (WSDL), and then by using that proxy to access the Web service in your Windows-based application.

By using Visual Studio

  1. In Solution Explorer, right-click ServiceConsumer, and then click Add Web Reference.

    The Add Web Reference dialog box appears.
  2. In the Address bar, type the TestWebService URL, and then press ENTER. For example, if
    TestWebService is located in the root directory of a Web server, type the following address:
  3. After TestWebService is located, click Add Reference.
  4. In Solution Explorer, right-click Form1, and then click
    View Code.
  5. Add the following namespace at the top of the Form1.vb file.
    Imports ServiceConsumer.<WebReferenceName>
    Note <WebReferenceName> is the name of the Web reference as it appears under Web References in Solution Explorer.

By using the WSDL Tool

  1. Open a Command Prompt window in Visual Studio.
  2. Change the folder to the location where you created the ServiceConsumer application.
  3. Type the following command:
    WSDL "http://WebServerName/TestWebService/Service1.asmx" /l:VB
    Service1.vb is created.
  4. In Solution Explorer, right-click ServiceConsumer, point to Add, and then click Add Existing Item.
  5. Locate and then click the Service1.vb file that you created in step 4. Click Open.

Consume the Web service in the Windows-based application

  1. In Solution Explorer, right-click Form1, and then click
    View Code.
  2. Add the following code to Form1.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Create an instance of the Web service.
    Dim myService As New Service1()
    ' Consume the Web Service method.
    End Sub
  3. On the Debug menu, click
    Start to run the application.
  4. Click Button1.

    "Hello World" is displayed.


For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Programming the Web with XML Web services
Walkthrough: Calling XML Web services from Windows Forms

ID do Artigo: 818364 - Última Revisão: 14 de jul de 2008 - Revisão: 1