Tim Kranen
Tim Kranen

Reputation: 4222

How to handle Async method that returns Void with WCF service on Windows Phone

I've created a client app on Windows Phone (8.0) that consumes a WCF service. I've added the service to the project successfully using Add Service Reference.. Now I want to call the methods asynchronously but I just can't figure out how to do that in VB. I've seen a lot of C# examples that I understand but somehow I can't figure it out in VB, since I am not much of a VB programmer.

What I do is create the client like this:

client = New ISSCompanionAppServiceClient("ISSCompanionApp")

Where "ISSCompanionApp" is the endpoint. Now I can call the method: client.GetSecurityTokenAsync(username, password) which is a valid method in the service. But the method doesn't return a value. How can I pass a callback in this method? The service does have some Event handlers defined like: GetSecurityTokenCompleted(Object, GetSecurityTokenCompletedEventArgs) but how do I implement this?

I've searched all over the web but all examples are either not applicable or in C#...

Upvotes: 0

Views: 320

Answers (1)

Tim Kranen
Tim Kranen

Reputation: 4222

I hate to answer my own questions but I found the solution after some more searching!

This is the code I used to solve the problem:

 Public Shared Sub Login(ByVal username As String, ByVal password As String)
    Dim client = New ISSCompanionAppServiceClient("ISSCompanionApp")
    AddHandler client.GetSecurityTokenCompleted, AddressOf GetSecurityTokenCallback
    client.GetSecurityTokenAsync(username, password)
End Sub

Private Shared Sub GetSecurityTokenCallback(ByVal sender As Object, ByVal e As ISSService.GetSecurityTokenCompletedEventArgs)
    MessageBox.Show(e.Result)
End Sub

So I needed to add a Handler to the client.

Upvotes: 1

Related Questions