1

Retrieving records from Azure Storage account table using c# while developing cross platform app using xamarin giving error.

await tbl.ExecuteAsync(retrieveOperation);

I AM FOLLOWING THIS LINK!

This code gives exception (Time awaiting and Async error using storageexception) when ILogin() is executed separately i.e not through the Logmein().

When I run Logmein() with the code in current form, nothing happens i.e not exception error occurs.

When i use task.Wait(); exception/error occurs related to wait. I'll try to provide exception details.

My code is here

    private void Logmein()
     {

             var task = Task.Run(async () => { await Ilogin(); });// Nothing happens by this code
             //task.Wait(); It gives error/Exception.}
private static async Task Ilogin()
    {

        // Rest of the code is right (TESTED)
        Microsoft.WindowsAzure.Storage.Table.CloudTable table = tableClient.GetTableReference("user");

        Entry uname = new Entry();
        Entry pword = new Entry();
        Label linfo = new Label();
        string userid = uname.Text.ToString();
        string passcode = pword.Text.ToString();
        Reg signuser = new Reg(userid);
        TableOperation retrieveOperation = TableOperation.Retrieve<Reg>("regform", userid);
        TableResult retrievedResult = await tbl.ExecuteAsync(retrieveOperation); //This AWAIT is making trouble
        Reg loginuser = (Reg)retrievedResult.Result;
        string u1 = loginuser.username.ToString();
        string p1 = loginuser.password.ToString();

        if (u1 == userid && p1 == passcode)
        {
            linfo.Text = "Login Successful";
            //await Navigation.PushAsync(new MainPage());
        }
        else
        {
            linfo.Text = "Invalid Username or Password";
        }
    }

// here is my exception handling code

catch (Exception ex)
         {
             linfo.Text = "login Login Error" + ex.Message + ex.StackTrace + ex.HelpLink + ex.Source;
         }

THANKS in advance.

Aslam Ranjha
  • 63
  • 1
  • 9
  • What error? On what line? Unfortunately, as written, there are no specifics to work with; they'd require someone loading/running your code to see the error. Please edit to be specific. – David Makogon Jun 30 '18 at 21:01
  • post the error detials and maybe `await tbl.ExecuteAsync(retrieveOperation);` – Pavneet_Singh Jun 30 '18 at 21:03
  • //task.Wait(); It gives error/Exception. Please see ON LINE # 2 – Aslam Ranjha Jun 30 '18 at 21:11
  • Panveet_Singh yup. I think this line is making trouble because of "await" – Aslam Ranjha Jun 30 '18 at 21:14
  • David Makogon, I have tested is many times with try/catch. Nothing is happening by running this code. – Aslam Ranjha Jun 30 '18 at 21:19
  • @AslamRanjha - Please *edit your question* - don't post details in comments. Also: Line 2 just says you have an exception (I saw that when I first read the question, which is why I asked you to include the exception). I mean, literally, include the actual exception, exactly as received. – David Makogon Jun 30 '18 at 21:26
  • actually i was editing the comment, got busy, in android, you should not put your app in waiting state, you will see a creahs if you do, use callback mechanism https://stackoverflow.com/a/10155677/4936904, once you have response , do what you want – Pavneet_Singh Jun 30 '18 at 21:31
  • Thanks Pavneet_Singh I try to follow it. – Aslam Ranjha Jun 30 '18 at 21:39
  • If you solved your own problem, put the solution in the Answer box below so that someone else can learn from it. You can mark your question as answered by accepting your own answer after a day or so. – Dour High Arch Jul 05 '18 at 16:12
  • @Dour High Arch ok. Right. – Aslam Ranjha Jul 05 '18 at 16:53

1 Answers1

0

Either go asynchronous all the way or synchronous all the way. Try to avoid mixing the two.

Task Ilogin() is an asynchronous method while void Logmein() is not

it should be made async and then awaited

private async Task Logmein() {
    await Ilogin();
}

There is no need for the Task.Run with fire and forget methods as their exception cannot be caught. Plus mixing blocking calls like Task.Wait() and Task.Result can lead to deadlocks.

if Logmein() needs to be invoked try putting it in an event handler, which would allow for async void so that it can be awaited.

private async void SomeHandler(object sender, EventArgs args) {
    await Logmein();
}

If that is the case you can avoid Logmein() and await Ilogin() directly.

Reference Async/Await - Best Practices in Asynchronous Programming

Nkosi
  • 235,767
  • 35
  • 427
  • 472