Encoding/Decoding Web Service: Service Consumer

This article, the second of two parts, configures a client which consumes the web service project that was created in part one. The consuming client can be another web application, or even a console application.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
January 17, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Introduction

This tutorial takes you step by step through creating a proxy client that can interrogate the web service, and retrieve results sent to you by the web service. The proxy client (the one that uses or consumes the web service), meaning you, will send a text string to be encoded (decoded) to the web service created in part one. The web service will then take your string and turn it into its encoded (decoded) form, and send it back to the proxy. The proxy will communicate to you the information received from the web service. This web service and  consuming client application's interaction may be schematically shown as in this figure.

The detailed sequence of events are as described below:

1. The client executes a process in the code written for the proxy.

2. The proxy receives the call and packages a request to be sent to the web service, including the parameters needed by the code as specified by the consumer, if any.

3. The function call is then routed to the web service, usually at a different location, but it could be any place to which the client has some connectivity. In the figure it is shown as taking place across the Internet.

4. The web service uses the parameters it receives and executes the process defined by the web service.

5. The web service sends the result of the process to the proxy. The proxy parses the XML sent by the web service and presents the value to the consuming client.

Generating a proxy

We know that the client communicates with the web service through the proxy. So how is this proxy created or generated? This takes place using the WSDL protocol. Referring back to the web service described in part one, we know that the asmx file in the Visual Studio IDE we created, is the URL of the web service. The WSDL, or the language that describes the web service, is simply obtained by appending ?WSDL to this URL. This resulting XML file describes the service in detail, and is usually very large. A sample of this [http://localhost/Test /Service1.asmx ?wsdl] is shown for the asmx file we created in part one here. For further details on WSDL please follow this link. This description lays out in detail the functions that are called and the protocols that carry the message.

Proxy class can be created using the command line executable wsdl.exe. It has a large number of switches to accommodate the various parameters, such as authentication information, connectivity information, URL, and so on. However, Visual Studio 2003 IDE has a visually appealing way of creating the proxy class. In this tutorial we will be looking at Visual Studio IDE (the easy way out!) to create the proxy class. Also, we will be using a Windows application to access the web service.

Service Consumer Creation 1-2-3

One: Start a project and add a web reference

Let's start a new project, a Windows application. Here it is called TestEncodeVB. The project comes with a default form, Form1, which we keep as is (though this is not a good practice). As it stands now,  it has no link to any web service. We create a proxy class by adding a web reference. This reference is the reference to the asmx file we created for the web service described in part one. How do we do it? Right click the project and from the drop down,  choose Add Web Reference... as shown in this picture.

This brings up the following dialogue, a utility screen  for adding the proxy as shown in this picture. Web services proliferate on the network, local machine, and so on. You need to browse to the service, or you can use the UDDI directory to find the service. I encourage the reader to experiment with all the possibilities.

Since we know that the service exists on this machine at http://localhost/Test /Service1.asmx, we can go ahead and type this in at the URL. On the other hand, we may click on the drop-down arrow which will bring up all the services on the local machine, from which we choose the one that we are interested in as shown:

The program searches for the URL and if it succeeds, as in this case, it shows the location where it found the service, as shown.

Now just click the Add  Reference... button, and this adds the web reference to your application as shown in the project view.  Since the web reference is a part of the project, the proxy class is added to the project TestEncodeVB.:

Two: Creating the user interface

The Form1 is the canvas we will be using for accessing the web service. This form must refer to the proxy class by importing as defined by this imports statement added at the top of the TestEncodeVB. vb file. The localhost has the Service1.asmx file. 

Imports TestEncodeVB.localhost 

If you recall the Hencode web service project, it had two methods defined in it, the Enc and the Denc for encoding and decoding respectively. We will code the client so that we can access either of these methods. The Form1 will have a couple of text boxes, and command buttons to facilitate passing parameters (strings in this case) to the Enc and Denc methods and displaying the results returned by the service. The next picture shows the Form1 with all the controls added.

The Visual Studio IDE is very well engineered with the drop down cues which facilitate the coding. The onclick events of these buttons are coded as shown here. Once you get the reference to the proxy class you can access the methods Enc and Denc through Service1.enc() and Service1.denc().

'For encoding
Private Sub Button2_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
If TextBox1.Text = "" Then
MsgBox("You insert a string in the box above")
End If
Dim strg As String
strg = Trim(TextBox1.Text)
Dim mysvc As New Service1
TextBox2.Text = mysvc.enc(strg)
End Sub
'For decoding
Private Sub Button1_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
Button1.Click
If TextBox3.Text = "" Then
MsgBox("You insert a string in the box above")
End If
Dim mysvc As New localhost.Service1
Dim strg As String
strg = Trim(TextBox3.Text)
TextBox4.Text = mysvc.denc(strg)
End Sub
This completes the coding of the user interface to access the web service.

Three: Testing the Client

Build the project, and since it is a Windows project, use the menu item right next to the Debug textbox and click to start. The program starts after a brief interval depending on the network, and brings up the Form1 for user input. I just entered mysorian in the text to encode box and clicked the Encode button. The encoded result appears in the text box shown for the returned result as bXlzb3JpYW4=.  Now I cut and paste the encoded text to test the functionality of the decode function, and I recover the original as shown.

Summary

In part one, creating a web service project was discussed in detail. This tutorial builds on the previous part to configure a client which consumes the service in part one. Details of how to get a reference to the proxy using the Visual Studio 2003 IDE were discussed. The consuming client can be another web application, or even a console application. If you need detailed source code for parts one and two, send me an email.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials