简体   繁体   English

使用Xamarin开发跨平台应用程序时使用C#从Azure存储帐户表中检索记录,并给出错误

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

Retrieving records from Azure Storage account table using c# while developing cross platform app using xamarin giving error. 使用xamarin开发跨平台应用程序时,使用c#从Azure存储帐户表中检索记录,并给出错误。

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 ie not through the Logmein(). 当分别执行ILogin()而不是通过Logmein()时,此代码会给出异常(使用storageexception的等待时间和异步错误)。

When I run Logmein() with the code in current form, nothing happens ie not exception error occurs. 当我以当前形式的代码运行Logmein()时,什么也不会发生,即不会发生异常错误。

When i use task.Wait(); 当我使用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. 提前致谢。

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 Task Ilogin()是一个异步方法,而void Logmein()不是

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. 不需要Task.Run并运行方法,因为无法捕获其异常。 Plus mixing blocking calls like Task.Wait() and Task.Result can lead to deadlocks. 加上混合使用诸如Task.Wait()Task.Result类的阻塞调用可能会导致死锁。

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. 如果需要调用Logmein() ,请尝试将其放在事件处理程序中,这将允许async void以便可以等待它。

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

If that is the case you can avoid Logmein() and await Ilogin() directly. 如果是这种情况,您可以避免Logmein()并直接等待Ilogin()

Reference Async/Await - Best Practices in Asynchronous Programming 参考Async / Await-异步编程最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在跨平台的Xamarin项目(iOS和android)中使用C#从查询列表中检索数据时面临参数异常错误 - Facing Arguments exception error on retrieving data from list of a query using c# in a cross platform xamarin project (ios and android) 使用 C# 中的 HtmlAgilityPack 为跨平台 Xamarin 应用程序解析 html - Parsing html using HtmlAgilityPack in C# for a cross-platform Xamarin app 使用azure函数从azure存储表中检索数据 - Retrieving data from azure storage table using azure functions C#Xamarin跨平台 - C# Xamarin Cross Platform 使用 C# SDK 定期刷新 Azure 存储帐户密钥 - Refresh Azure Storage Account Key Periodically using C# SDK Azure 存储帐户使用托管标识和 C# 进行身份验证 - Azure Storage Account authenticate using Managed Identity and C# C#Xamarin无法使用Xamarin跨平台项目扩展Web视图 - c# xamarin unable to expand the web view using xamarin cross platform project 如何在 c#(xamarin 跨平台应用程序)中获取屏幕尺寸 - How to get screen dimensions in c# (xamarin cross platform app) C# - 在本机应用程序上将 Azure Key Vault 与 Azure 存储结合使用 - C# - Using Azure Key Vault with Azure Storage on Native App 使用xamarin.forms跨平台C#在设备日历上设置提醒 - Set reminder on device calendar using xamarin.forms cross platform C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM