1
votes

I am trying to call a simple Login function in WCF service from Xamarin Forms.

I have referenced the service with no problems.

I have defined the client object like this:

Service1Client client = new Service1Client();

My service function name is Login and returns a String. Here is the definition:

Public Function Login(ByVal wUser As String, ByVal wPassword As String) As String Implements IService1.Login
    Return String.Format("Test")
End Function

When I try to call that function from Xamarin Forms ServiceClient object I only have access to LoginAsync function instead of Login. Researching a bit I saw that I have to use await with async functions.

I tried that but then the line displays an error "Can't use await with void":

await client.LoginAsync("user", "password");

What I am doing wrong?

If I remove the await statement the WCF functions executes correctly and returns the "Test" string, but I do not know how to retrieve that on Xamarin Forms.

How can I retrieve the string that the function is trying to return?

Thank you.

1
have you tried "string value = await client.LoginAsync("user", "password");"Jason
Yes, I have tried that but the error with await "cant't use await with void" is still there. I do not understand this because my function is of type string not void. If I remove await then the error is "Can not convert from void to string".Michelh91
what is the signature of the LoginAsync method? It may require a callback method rather than using async.Jason

1 Answers

2
votes

I finally found a solution.

I needed to asign a listener function to LoginCompleted event.

  client.LoginCompleted += client_LoginCompleted;
  client.LoginAsync("user", "password");

And then I can get the result in the EventArgs of that function

 static void client_LoginCompleted(object sender, LoginCompletedEventArgs e)
    {
        var result = e.Result;
    }

Here is the link that helped me to understand how async call works in WCF and the three posible ways of getting the result.

http://jaliyaudagedara.blogspot.com.es/2013/03/asynchronous-operations-in-wcf.html

Hope this helps someone.