Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to invoke a Web Services Asynchronously from a VB.Net desktop application. My code is included.

When the button is clicked I call the function KycCustomerCheck(), which grabs the required parameters from my form and invokes the service method Asynchronously. After the method is invoked I add an Event Handler that calls the function getCDDresults, which fetches the method results and writes them to a DataGridView on my form.

When I test the Application, the Web Service is successfully invoked and executed. However, my Event Handler is not executed. My guess is that my event handler has not been implemented correctly. If someone can help me to resolve this I will be very grateful. Thanks in advance.

Sincerely,

Matthew Paisley
matt.paisley57@gmail.com

What I have tried:

VB
Public Class DueDilligence

    Public handlerAttached As Boolean = False

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button4.Click
        Dim success As Boolean, strSuccess As String
        Dim b As Button = DirectCast(sender, Button)

        Try
            Select Case b.Name
                Case "Button1"
					'**** this logic works 
                Case "Button4"
                    strSuccess = 	
                    If strSuccess = "Success" Then
                        txtSessionId.Text = "Processing Request"
                    End If
            End Select

        Catch ex As Exception
            txtSessionId.Text = "ERROR: " & ex.Message

        End Try

    End Sub

   Private Function KycCustomerCheck() As String
        Dim KYC As New CustomerDueDilligence.CustomerDueDilligenceService()
        Dim BPCredentialName As String = My.Settings.BPCredentialName
        Dim BPResourceName As String = My.Settings.BPResourceName
        Try
            KYC.Url = My.Settings.HostName & My.Settings.PortAssignment & "/ws/CustomerDueDilligence"
            KYC.Credentials = New System.Net.NetworkCredential(My.Settings.uid, My.Settings.pw)
            KYC.CustomerDueDilligenceAsync(txtFirstName.Text, txtLastName.Text,
                                           txtFirstName.Text & " " & txtLastName.Text,
                                           txtStreet.Text, txtCity.Text, txtState.Text, txtZipCode.Text)
            If Not handlerAttached Then
                AddHandler My.WebServices.CustomerDueDilligenceService.CustomerDueDilligenceCompleted,
                                AddressOf Me.getCDDresults
                handlerAttached = True
            End If

            Return "Success"
        Catch ex As Exception
            Return "Exception - " & ex.Message
        End Try
    End Function

    Private Sub getCDDresults(ByVal sender As Object, ByVal results As CustomerDueDilligence.CustomerDueDilligenceCompletedEventArgs)
        Dim row As String() = New String() {"Name", txtFirstName.Text & " " & txtLastName.Text}
        DataGridView1.Rows.Add(row)
        row = New String() {"Street", txtStreet.Text}
        DataGridView1.Rows.Add(row)
        row = New String() {"City", txtCity.Text}
        DataGridView1.Rows.Add(row)
        row = New String() {"State", txtState.Text}
        DataGridView1.Rows.Add(row)
        row = New String() {"Zip", results.Zip}
        DataGridView1.Rows.Add(row)
        row = New String() {"RealAddress", results.RealAddress}
        DataGridView1.Rows.Add(row)
        row = New String() {"CreditScore", results.CreditScore}
        DataGridView1.Rows.Add(row)
        row = New String() {"OFAC", results.OFAC}
        DataGridView1.Rows.Add(row)
        row = New String() {"DPL", results.DPL}
        DataGridView1.Rows.Add(row)
        row = New String() {"EUFSL", results.EUFSL}
        DataGridView1.Rows.Add(row)
        row = New String() {"INTERPOL", results.INTERPOL}
        DataGridView1.Rows.Add(row)
        row = New String() {"Status", results.Status}
        DataGridView1.Rows.Add(row)
    End Sub

End Class
Posted
Updated 13-Aug-20 10:25am

1 solution

I don't see the call like My.WebServices.XYZService.getXYZAsync() being made or post handler attached.

Example is in VB but hopefully will give the directions to go ahead. Try following steps:
1. Reference the XYZ Web service
2. Add an event handler for the getXYZCompleted event:
VB
Private Sub getXYZCompletedHandler(ByVal sender As Object,
    ByVal e As net.xmethods.www.getXYZCompletedEventArgs)

    MsgBox("XYZ: " & e.Result)
End Sub

3. Add a field to track if the event handler has been added to the getXYZCompleted event:
VB
Private handlerAttached As Boolean = False

4. Add a method to add the event handler to the getXYZCompleted event, if necessary, and to call the getXYZAsync method:
VB
Sub CallGetXYZAsync(ByVal zipCode As Integer)
    If Not handlerAttached Then
        AddHandler My.WebServices.
            XYZService.getXYZCompleted,
            AddressOf Me.TS_getXYZCompleted
        handlerAttached = True
    End If
    My.WebServices.XYZService.getXYZAsync(zipCode)
End Sub

To call the getXYZ Web method asynchronously, call the CallGetXYZAsync method. When the Web method finishes, its return value is passed to the getXYZCompletedHandler event handler.

Reference: How to: Call a Web Service Asynchronously - Visual Basic | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Member 14878480 13-Aug-20 17:27pm    
Thanks Sandeep for your prompt response.

I read the article that you have referenced before I built the code, which I have submitted. If you look at it for Item 2. I have provided:

Private Sub getCDDresults(ByVal sender As Object, ByVal results As CustomerDueDilligence.CustomerDueDilligenceCompletedEventArgs)

This is my handler that takes the Web Service results and writes them to my VB Form.

for Item 3. I have added a global variable "handlerAttached as Boolean".

for Item 4 I added the following to my function that executes after the WS has been instantiated:

If Not handlerAttached Then
AddHandler My.WebServices.CustomerDueDilligenceService.CustomerDueDilligenceCompleted,
AddressOf Me.getCDDresults
handlerAttached = True
End If

When I run my code the Web Services is instantiated and runs without exceptions. My problem is that the Event Handler is never executed.

Is there something that I need to add to the project configuration. I don't understand what I have done wrong. If you can take a closer look it will be greatly appreciated.
Sandeep Mewara 14-Aug-20 1:34am    
Step 4, there is an extra line in examples: My.WebServices.XYZService.getXYZAsync(zipCode)
Is that something missing?
Richard Deeming 14-Aug-20 6:38am    
You need to attach the event handler before calling the CustomerDueDilligenceAsync method. Otherwise, the call could complete before the event handler is attached, and the event will be lost.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900