[英]Azure Mobile Service client query not returning control back to Xamarin Form android client app
I am using Azure Mobile Service with Xamarin Form Android application to mostly query data from the azure table storage. 我正在使用带有Xamarin Form Android应用程序的Azure Mobile Service来主要查询来自azure表存储的数据。
The current problem what I am facing is the azure mobile service client is not returning the control back right after the mobile service client API call ( This has been the case only with the Portable class library and android app project, but the same call returns result in normal .net library as I have used test project to verify the API). 我面临的当前问题是azure移动服务客户端在移动服务客户端API调用之后没有立即返回控件(这只是使用Portable类库和android app项目的情况,但同样的调用返回结果在正常的.net库中,因为我已经使用测试项目来验证API)。
The source code I have used as follows: 我使用的源代码如下:
Azure Mobile Service code: Azure移动服务代码:
public class VerticalFarmController : TableController<VerticalFarm>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MobileServiceContext context = new MobileServiceContext();
string connectionString = "My_StorageConnectionString";
DomainManager = new StorageDomainManager<VerticalFarm>(connectionString, "VerticalFarm", Request, Services);
}
public Task<IEnumerable<VerticalFarm>> GetAllVerticalFarm(ODataQueryOptions queryOptions)
{
return base.QueryAsync(queryOptions);
}
}
Xamarin Form Android app code: Xamarin Form Android应用程序代码:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
private const string ApiUrl = "[Mobile service Url]";
private const string AppKey = "[Application key]";
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
IMobileServiceClient mobileServiceClient = new MobileServiceClient(ApiUrl, AppKey);
try
{
var table = mobileServiceClient.GetTable("verticalfarm");
var result = table.ReadAsync("$top=10", null, wrapResult: true).Result;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
LoadApplication(new App());
}
}
It neither returns result nor an exception right after the execution of following line of code: 在执行以下代码行之后,它既不返回结果也不返回异常:
var result = table.ReadAsync("$top=10", null, wrapResult: true).Result;
It would be great know if anybody had similar issue and were able resolve it. 如果有人有类似的问题并且能够解决它,那将是很好的。
Calling .Result like the following will cause a deadlock 调用.Result如下所示会导致死锁
var result = table.ReadAsync("$top=10", null, wrapResult: true).Result;
I had the same issue with MobileServicesClient.InvokeApiAsync() call 我在MobileServicesClient.InvokeApiAsync()调用时遇到了同样的问题
Instead you should await like:- 相反,你应该等待: -
async Task ReadTable()
{
var result = await table.ReadAsync("$top=10", null, wrapResult: true);
// do something with result
}
Or in my case 或者在我的情况下
async Task CallApi()
{
var response = await App.client.InvokeApiAsync ("/api/call", System.Net.Http.HttpMethod.Get, null);
// do something with response
}
The behaviour you're experimenting is normal since ReadAsync is an asynchronous method. 您正在尝试的行为是正常的,因为ReadAsync是一种异步方法。 To "wait" for the call to complete, you need to use the await keyword. 要“等待”完成调用,您需要使用await关键字。
var result = await table.ReadAsync("$top=10", null, wrapResult: true).Result; var result = await table.ReadAsync(“$ top = 10”,null,wrapResult:true).Result;
You can read more about async support in Xamarin by reading this Xamarin documentation: http://developer.xamarin.com/guides/cross-platform/advanced/async_support_overview/ 您可以通过阅读此Xamarin文档来阅读有关Xamarin中的异步支持的更多信息: http : //developer.xamarin.com/guides/cross-platform/advanced/async_support_overview/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.