Microsoft KB Archive/818364

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

Article ID: 818364

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Web Services Enhancements for Microsoft .NET 1.1
 * Microsoft Web Services Enhancements for Microsoft .NET 2.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
 Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project.

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

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

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

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.
 * 1) Under Template, click to select Windows Application.
 * 2) In the Name box, type ServiceConsumer, and then click OK.

By default, a form that is named Form1 is created.
 * 1) 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
<ol> In Solution Explorer, right-click ServiceConsumer, and then click Add Web Reference.

The Add Web Reference dialog box appears.</li> 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:// /TestWebService/Service1.asmx

</li> After TestWebService is located, click Add Reference.</li> In Solution Explorer, right-click Form1, and then click View Code.</li>  Add the following namespace at the top of the Form1.vb file. Imports ServiceConsumer.<WebReferenceName> Note  is the name of the Web reference as it appears under Web References in Solution Explorer. </li></ol>

By using the WSDL Tool
<ol> Open a Command Prompt window in Visual Studio.</li> Change the folder to the location where you created the ServiceConsumer application.</li> Type the following command:

WSDL &quot;http://WebServerName/TestWebService/Service1.asmx&quot; /l:VB

Service1.vb is created.</li> In Solution Explorer, right-click ServiceConsumer, point to Add, and then click Add Existing Item.</li> Locate and then click the Service1.vb file that you created in step 4. Click Open.</li></ol>

Consume the Web service in the Windows-based application
<ol> In Solution Explorer, right-click Form1, and then click View Code.</li>  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 </li> On the Debug menu, click Start to run the application.</li> Click Button1.

&quot;Hello World&quot; is displayed.</li></ol>

<div class="references_section">