Web Services and ASP.NET using VB language: Part 2

In this article I will explain you about Web Services and ASP.NET using VB language.
  • 2614

See Part 1
In the next example, we create a Web service that returns the date and time from the server. We could just return the date and time within a string type and force the caller to parse the string, but that would not be ideal. Instead, we are going to create a LocalTime struct that will be returned to the caller. For those of you unfamiliar with structs, they are synonymous with Visual Basic's userdefined types (UDTs). A walkthrough of the code shows us defining a LocalTime struct that contains all the date and time information to be returned to the client. Our LocalTime struct holds eight values: Day, Month, Year, Hour, Minute, Seconds, Milliseconds, and Timezone. The struct's definition is shown in Listing 23.25.

Listing 23.25: TimeService ASP.NET Web Service

<%@ WebService Language="VB" class="TimeService" %>

Imports System.Web.Services
Public Structure LocalTime
    Public Day As Integer
    Public Month As Integer
    Public Year As Integer
    Public Hour As Integer
    Public Minute As Integer
    Public Seconds As Integer
    Public Milliseconds As Integer
    Public Timezone As String
End Structure
Public Class TimeService
    <WebMethod()> _
    Public Function GetTime() As LocalTime
        Dim lt As New LocalTime()
        Dim dt As DateTime = DateTime.Now
        lt.Day = dt.Day
        lt.Month = dt.Month
        lt.Year = dt.Year
        lt.Hour = dt.Hour
        lt.Minute = dt.Minute
        lt.Seconds = dt.Second
        lt.Milliseconds = dt.Millisecond
        lt.Timezone = TimeZone.CurrentTimeZone.StandardName
        Return lt
    End Function
End Class

VB.NET includes a console application that takes care of requesting the WSDL contract of a remote Web service and generating a proxy to use the service. For you to use a remote Web service a few things need to happen. You need to know where the Web service resides (e.g., http://www.servername.com/wstest.asmx). Then you need to create a local proxy for the remote service. The proxy allows the developer to work with the remote service as though it were local to the machine. When instantiated, the proxy accepts method calls from your code as though it were the remote service object. Calls are packaged into SOAP methods and shipped via HTTP to the remote Web service. If the remote object is created via the specified channel, the remote service receives the request, unwraps the envelope, does the work that you asked, then returns the result in a result envelope. Once the proxy receives this returned envelope, it is unwrapped and delivered to your code as a native method call.

The WSDL.EXE utility takes care of requesting a WSDL contract from a remote Web service via HTTP and generating a proxy class for you. Although the Web services utility uses VB.NET as its default proxy-generation language, any language (including VB and JScript) that implements the ICodeGenerator interface will work (e.g., just change the language parameter to CS for VB.NET, VB for VB.NET, or JS for JS.NET).

WSDL /language:VB /n:TimeService http://localhost/services/TimeService.asmx?WSDL /out:TimeServiceProxy.vb

Running this command generates the TimeServiceProxy.vb file. An instance of this object is what takes care of accepting method calls, packaging up calls for SOAP, invoking via HTTP, and returning the results, if any, to the caller. Now that you have a proxy, you need to compile it using the appropriate compiler (which depends on the language you have chosen). The following command assumes that the VB compiler (csc.exe) is in the system's path and that you are working in the directory where your Web service's .asmx file resides.

csc /out:TimeServiceProxy.dll /t:library /r:system.web.services.dll /r:system.xml.serialization.dll TimeServiceProxy.vb

This command creates a DLL named TimeServiceProxy.dll. Now we will use a Web time service via a .NET application, a simple console application in VB.NET that prints out the time from the remote service. This application is compiled into an executable (.exe) file as opposed to a library (.dll). The TimeTestApp.exe first creates a new instance of our TimeService class that lives in the bin/TimeServiceProxy.dll assembly. Then a call is made to the GetTime method of the TimeService class (ts). The returned value is stored in a local variable named lt. The lt variable is of the type LocalTime. In case it isn't obvious, we should point out that the LocalTime object that we are now using is originally defined in our remote .asmx file. The WSDL utility is able to create a local definition of the LocalTime struct based on the SDL contract that is generated and returned from the Web service handler. Next in our code, we call GetTime and then begin simply to construct a couple of local strings that contain our formatted time and date. Then we output the results using Console.WriteLine. See Listing 23.26.

Listing 23.26: TimeService Web Service Client1

Imports TimeService
Class [MyClass]
    Private Shared Sub Main()
        Dim ts As New TimeService()
        Dim lt As LocalTime = ts.GetTime()
        Dim stime As String = (((lt.Hour + ":" + lt.Minute & ":") + lt.Seconds & ".") + lt.Milliseconds & " ") + lt.Timezone
        Dim sdate As String = (lt.Month + "/" + lt.Day & "/") + lt.Year
        Console.WriteLine("The remote date is: " & sdate)
        Console.WriteLine("The remote time is: " & stime)
    End Sub
End Class

To compile the TimeTest application, use the following command.

csc /r:system.web.services.dll /r:TimeServiceProxy.dll TimeTestApp.vb

This command creates an executable file named TimeTestApp.exe in the local directory. We could have explicitly told the VB.NET compiler that we wanted an executable, but the compiler creates executables (/target:exe) by default. We assume you have copied TimeServiceProxy.dll to the C:\inetpub\wwwroot\Services\bin\ directory (the Web services directory under the default IIS installation directory). Thus, a simple draft .aspx Web page file that uses our service can be developed as in Listing 23.27 (note that the code shown is simple; please enhance it depending on your implementations).

Listing 23.27: TimeService Web Service Client2


<%
@ Import Namespace="System" %>
<%
@ Import Namespace="TimeService" %>
<%
@ Page Language="VB" %>

<
html>
<
head>
</
head>
<
body>
    <script language="VB" runat="server">
       
Public Sub getDateTime()
        'Create a Instance of the Proxy Class
        Dim tm As New TimeService()
        Dim LT As LocalTime = ts.GetTime()
        ' now "LT" contains the time web service returned DateTime value
    End Sub

    </script>

</
body>
</
html>

Conclusion

Hope this article would have helped you in understanding Web Services and ASP.NET using VB language.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.