Encoding/Decoding Web Service: Web Service Provider - Configuring the Web Service
(Page 3 of 4 )
In the VS 2003 create a new project by choosing File --> New--> Project from the File Menu item. This opens up the New Project window and highlights the Web Services as shown below. The default web service is WebService1. Change this to something different; in the present tutorial it is Hencode.

The project explorer view with this choice is shown here. To the usual references you will see the System.Web.Services reference is also added. The Service1.asmx is the file that will contain the web methods that we are going to code.

Service1.asmx is the file that will contain the web methods. This file has a design view as well as a code view, Service1.asmx.vb. We will not be using the design view but our code will be inserted into the Service1.asmx.vb pane.

In VS 2003 the Service1.asmx will come with a sample method (although commented out), the well known HelloWorld() method. What we will be doing is creating two methods, one of them for encoding and the other for decoding functionality. Arguments have to be passed to these functions from the calling program (the Client). We will be looking at the client in part 2.
Let's look at the default, Public Function HelloWorld() web method. All this function does is display "HelloWorld" to the client. It is very important to use the correct syntax. If there are more methods you will be calling more functions as we shall see later.
'WEB SERVICE EXAMPLE
' The HelloWorld() example service returns the string Hello
World.
' To build, uncomment the following lines then save and build the
project.
' To test this web service, ensure that the .asmx file is the
start page
' and press F5.
'
'<WebMethod()> _
'Public Function HelloWorld() As String
' Return "Hello World"
'End FunctionIn order to get the encoding functionality we will create an encoding function, enc() which takes a string as an argument and returns a string. For the reverse case we will be passing a string to a function called denc() that we create, which will provide a decoded string back. In each of these cases we will be using the methods of the System.Text.ASCIICoding.ASCII class for proper conversions and encoding/decoding. The two web methods are shown here:
<WebMethod()> _
Public Function enc (ByVal StrEncode As String)
Dim encodedStr As String
encodedStr = _
Convert.ToBase64String(System.Text.ASCIIEncoding. _
ASCII.GetBytes(StrEncode))
Return (encodedStr)
End Function
<WebMethod()> _
Public Function denc (ByVal StrDecode As String)
Dim decodedStrg As String
decodedStrg = System.Text.ASCIIEncoding.ASCII. _
GetString(Convert.FromBase64String(StrDecode))
Return decodedStrg
End Function
With this accomplished the Web Service provider is complete. The next section shows how you may test it within the VS 20043 IDE. However, for client access you may have to build a project so that they may access this service over the web. That will be described in part 2.
Next: Local Testing >>
More Visual Basic.NET Articles
More By Jayaram Krishnaswamy