Login page to call a WCF service and get a response when done ASYNC

556 Views Asked by At

Well... I have a Login page and a WCF implanting the Event Based Async pattern. Eventually what I need to do here is to make my async call to the WCF method and the execution should wait for the response before continuing with the authentication or not and then redirect.

I am developing on WP8 and I call my WCF this way:

Login.xaml.cs

private async void SignIn_Click(object sender, EventArgs e)
{
   await App.Instance.StaticServiceData.LoadUser(App.Instance.User);

   LoginSuccessFail();
}

WCF_StaticDataService.cs

public async Task LoadUser(User user)
{
   var client = new BMAStaticDataService.StaticClient();
   client.AuthenticateUserAsync(user);
   client.AuthenticateUserCompleted += async (o,e) => {
   try
   {
      await UpdateCacheUserData(existing);
      if (existing.UserId > 0)
         {
            App.Instance.User.UserId = existing.UserId;
            App.Instance.User.Email = existing.Email;

            App.Instance.IsUserAuthenticated = true;
         }
      else
      {
         App.Instance.IsUserAuthenticated = false;
         throw new Exception("User has no authentication");
      }
   }
   catch (Exception){throw;}
}

I want to call the LoginSuccessFail(); method and then navigate to the appropriate page only after the WCF call is completed. Note that I wouldn't like to put my navigation aware code in the Service class and the AuthenticateUserCompleted event since it is not its job to handle this.

Eventually I would like to receive a callback or feedback or whatever in my Login.xaml.cs after the service call is done, successfully or not.

Hope my info is enough. Otherwise please advise to provide further clarification.

0

There are 0 best solutions below