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



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Web Services (included with the .NET Framework 1.1)
  • Microsoft Web Services (included with the .NET Framework) 1.0

SUMMARY

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 File menu, point to New, and then click Project.

    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

  1. Start Visual Studio 2005 or Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Type, click to select Visual Basic Project.

    Note In Visual Studio 2005, click to select Visual Basic instead of Visual Basic Project.
  4. Under Template, click to select Windows Application.
  5. In the Name box, type ServiceConsumer, and then click OK.

    By default, a form that is named Form1 is created.
  6. Add a Button control to Form1.

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:

    http://WebServerName/TestWebService/Service1.asmx

  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.
       MessageBox.Show(myService.HelloWorld())
    End Sub
  3. On the Debug menu, click Start to run the application.
  4. Click Button1.

    "Hello World" is displayed.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:

Walkthrough: Calling XML Web services from Windows Forms
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconwindowsformswebservices.asp


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbWindowsForms kbWebServices kbHOWTOmaster KB818364 kbAudDeveloper