我正在使用带有Xamarin Form Android应用程序的Azure Mobile Service来主要查询来自azure表存储的数据 .
我面临的当前问题是azure移动服务客户端在移动服务客户端API调用之后没有立即返回控件(这只是使用Portable类库和android app项目的情况,但同样的调用返回结果在正常的.net库中,因为我使用了测试项目来验证API) .
我使用的源代码如下:
Azure Mobile Service code:
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:
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());
}
}
在执行以下代码行之后,它既不返回结果也不返回异常:
var result = table.ReadAsync("$top=10", null, wrapResult: true).Result;
如果有人有类似的问题并且能够解决它,那将是很好的 .
2 回答
调用.Result如下所示会导致死锁
我在MobileServicesClient.InvokeApiAsync()调用时遇到了同样的问题
相反,你应该等待: -
或者在我的情况下
您正在尝试的行为是正常的,因为ReadAsync是一种异步方法 . 要“等待”完成调用,您需要使用await关键字 .
var result = await table.ReadAsync("$top=10",null,wrapResult:true).Result;
您可以通过阅读此Xamarin文档来阅读有关Xamarin中的异步支持的更多信息:http://developer.xamarin.com/guides/cross-platform/advanced/async_support_overview/