6.2. Make a SOAP client in VB .NET using the command-line

Like for the C# exemple, we begin by running the wsdl tool to generate our proxy. Run the tool as showed below for my on-line JWSDP web service. You can also replace the url pointing to the WSDL of my web service by one pointing to another valid WSDL document.

[your_prompt]:\> wsdl /l:vb /protocol:SOAP 
http://www.pascalbotte.be/rcx-ws/rcx?WSDL

The tool will generate, for the case of my web service, the MyRcxService class (in the MyRcxService.vb VB file). We will need this file to compile our client.

6.2.1. Consume remote procedures returning simple type

Now create our client VB file and copy/paste the code listed below:

Example 6-3. Simple VB .NET SOAP client

In this example, We will call three simple remote procedure:

  • readLS(): this method take a string as param and return a string. This is an "echo string" demo.

  • readLSpercent(): this one take an int type as param and return an int. This method is supposed to return a real-time value of the amount of light (by the use of a light sensor) in percent (if my web service is up).

  • readTemp(): and this one take an int as param and return a float, which is the temperature (°C) in my office (again, if my web service is up).


Imports System

' add this import for vbCrLf constant
Imports Microsoft.VisualBasic

Module Main

  Sub Main()

    ' create the Proxy
    Dim mrs As New MyRcxService()
    
    ' use the wrapper classes for
    ' the params for document/literal
    ' web service
    Dim rls As New readLS()
    Dim rlsp As New readLSpercent()
    Dim rt As New readTemp()

    ' initialize param for the 
    ' readLS() remote procedure
    ' See [1] for rpc/encoded
    rls.String_1 = "Your e-mail or message here"
    ' for each call use the wrapper
    ' classes for the return value
    Dim rlsresp As readLSResponse
    rlsresp = mrs.readLS(rls)
    Console.WriteLine("Message: " + rlsresp.result)
	
    ' readLSpercent() call
    rlsp.int_1 = 1
    Dim rslpresp As readLSpercentResponse		
    rslpresp = mrs.readLSpercent(rlsp)
    Console.WriteLine("Light sensor value: " + rslpresp.result.ToString)

    ' readTemp() call
    rt.int_1 = 1
    Dim rtresp As readTempResponse
    rtresp = mrs.readTemp(rt)
    Console.WriteLine("Temperature: " + rtresp.result.ToString)	
	
  End Sub
End Module

Now you can compile using the command-line compiler for VB.NET: vbc.exe

[your_prompt]:\> vbc client.vb myrcxservice.vb

The compiler will generate a portable executable (.exe) file, in a mono-file assembly. You should be able to run it on any computer having the .NET runtime installed.

In the following sections we will create soap clients handling complex types.

6.2.2. Consuming SOAP procedures returning complex type with the .NET framework SDK and VB

Handling complex type with VB .NET is easy. You will find example code to consume procedure returning an object, an array of simple type and an array of object.

Add the code below to consume the following remote procedure:


  ' Use the wrapper class for the param
  ' and the response
  Dim st As New status()
  Dim stresp As statusResponse
  ' the object itself
  Dim rresp As RcxResponse
  
  ' call the method status()
  stresp = mrs.status(st)
  ' use the RcxResponse object
  ' to access the elements
  rresp = stresp.result
  Console.WriteLine("Battery level: " & rresp.battery & _
    vbCrLf & "Internal clock: " & rresp.currentTime & _
    vbCrLf & "Free memory: " & rresp.memory & _
    vbCrLf & "Total memory: " & rresp.memoryTot & _
    vbCrLf & "RCX message: " & rresp.status)

  ' wrapper classes only for the param,
  ' we can use an array of integer for
  ' the return value
  Dim ciresp() As Integer
  Dim ci As New collInt()

  ' call collInt() method
  ciresp = mrs.collInt(ci)
  Dim x As Integer
		
  For x=0 To (ciresp.Length - 1)
    Console.WriteLine("Element " + cstr(x) + ", value: " + cstr(ciresp(x)))
  Next

  ' wrapper for the null param
  ' array of object for the 
  ' return value
  Dim cp As New collPos()
  Dim cpar() As PosCol

  cpar = mrs.collPos(cp)
  Dim cpel As PosCol

  For x=0 To (cpar.Length - 1)
    cpel = cpar(x)
    Console.WriteLine("object: " + cstr(x) + _
      ", XPos: " + cstr(cpel.XPos) + ", YPos: " + cstr(cpel.YPos))
  Next
			

So, now you can use VB .NET, even without the IDE (Visual Studio .NET), to make a SOAP client! You can edit your code with NotePad, or use a free IDE like SHARPDEVELOP. This wonderfull IDE will let you edit, compile and debug C# and VB .NET code with the free .NET framework SDK. See the home page of #DEVELOP.

Notes

[1]

Again, no need of the wrapper classes for an rpc/encoded web service. Follow the example below:

' the function below will return a string
mrs.readLS("Your message or e-mail here")