Await AsyncCallback of BackgroundWorker for Webservice reference Function VB.Net

I’m trying to build a new class from where I can call functions with a background worker from a web service. It is a windows Phone 8.1 silverlight app.

calling page

LoginPage.xaml.vb
Partial Public Class LoginPage
Private sub Logon()        
        WebServiceHelper.a(WebServiceHelper.Functions.Logon)
        LoginFinish()        
End Function
End Class

WebserviceHelper

WebServiceHelper.vb
Public Class WebServiceHelper

Public Shared Function a(ByVal Task1 As Functions)
    If _Task = Functions.Logon Then
        _Service.BeginLogonWindowsPhone(usr, pass, New AsyncCallback(AddressOf ResultBackGroundTask), result)    
    End If
End Function

Public Shared Sub ResultBackGroundTask(ByVal result As Object) 
    If _Task = Functions.Logon Then
        ResultObject = result
    End If
End Sub
End Class

The problem is when I call webservice.a() the AsyncCallback ResultBackGroundTask doesn’t fire in time. Instead LoginFinish is called resulting in an error because the resultobject isn’t initialized yet.

I’ve tried:

Task.Factory.FromAsync(_Service.BeginLogonWindowsPhone, ResultBackGroundTask, usr, pass, Nothing)

But I get an error:

Argument not specified for parameter 'asyncState' of
'Public Function BeginLogonWindowsPhone(userName As String, password As String, callback As System.AsyncCallback, asyncState As Object) As System.IAsyncResult'.

The function I try to call is:

<System.ServiceModel.OperationContractAttribute(AsyncPattern:=true, Action:="http://test.com/LogonWindowsPhone", ReplyAction:="*"),  _
     System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults:=true)>  _
Function BeginLogonWindowsPhone(ByVal userName As String, ByVal password As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult

This function is automatically generated in the webservice reference.


Source: .net

Leave a Reply