Consuming web service from VB 6.0 with custom data type

How do you call a web service from VB6.0 and access an object returned from the web method?  This is how you do it.

You need to declare the object type to IXMLDOMSelection class defined in MSXML library.   Note that you need to add reference to appropriate version MSXML library available in your environment.

Web Service

Public Class MyCustomClass

    Public Name As String

    Public Age As Integer

    Public DOB As DateTime

End Class


<WebMethod()> _

    Public Function HelloWorld() As MyCustomClass

        Dim obj As New MyCustomClass

        obj.Name = "John"

        obj.Age = 21

        obj.DOB = Now

        Return obj

    End Function

VB6 Client

Private Sub Command1_Click()

'Dim x As New MSSOAPLib30.SoapClient30

    Dim objSClient As New MSSOAPLib30.SoapClient30

    Dim x As MSXML2.IXMLDOMSelection

    Dim result As Object

    'Set objSClient = CreateObject("MSSOAPLib30.SoapClient30")

    objSClient.MSSoapInit ("http://localhost/myservice.asmx?wsdl")

    objSClient.ConnectorProperty("Timeout") = "120000"  'in Milliseconds

    Set x = objSClient.HelloWorld()

    MsgBox "Name : " & x.Item(0).nodeTypedValue

    MsgBox "Age : " & x.Item(1).nodeTypedValue

    MsgBox "Dob : " & x.Item(2).nodeTypedValue

End Sub

If you need any clarification, please feel free to call me at ext. 5061

No comments: