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
SUMMARYThis 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://WebServerName/TestWebService,
and then click OK.
Note WebServerName 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 = "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. - On the Build menu, click Build
TestWebService.
Create a Windows-based application- Start Visual Studio 2005 or Visual Studio .NET.
- On the File
menu, point to New, and then click
Project.
- Under Project Type, click to select Visual
Basic Project.
Note In Visual Studio 2005, click to select Visual
Basic instead of Visual
Basic Project. - Under Template, click to select
Windows Application.
- In the Name box, type
ServiceConsumer, and then click OK.
By default, a form that is named Form1 is created. - Add a Button control
to Form1.
Add the Web service
reference to the Windows-based applicationYou 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- In Solution Explorer, right-click ServiceConsumer, and
then click Add Web Reference.
The Add Web
Reference dialog box appears. - 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 - After TestWebService is located, click Add
Reference.
- In Solution Explorer, right-click Form1, and then click
View Code.
- 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- Open a Command Prompt window in Visual Studio.
- Change the folder to the location
where you created the ServiceConsumer application.
- Type the following command:
WSDL "http://WebServerName/TestWebService/Service1.asmx" /l:VB Service1.vb is created. - In Solution Explorer, right-click ServiceConsumer,
point to Add, and then click Add Existing
Item.
- 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- In Solution Explorer, right-click Form1, and then click
View Code.
- 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 - On the Debug menu, click
Start to run the application.
- Click Button1.
"Hello World" is
displayed.
REFERENCESFor more information, visit the following Microsoft Developer Network (MSDN) Web
sites:
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbWindowsForms kbWebServices kbHOWTOmaster KB818364 kbAudDeveloper |
---|
|