[英]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.