我将 Azure Mobile Service 与 Xamarin Form Android 应用程序结合使用,主要是从 azure 表存储中查询数据。
我目前面临的问题是 Azure 移动服务客户端没有在移动服务客户端之后立即返回控制权 API 调用(只有可移植类库和 Android 应用程序项目才会出现这种情况,但相同的调用会在正常的 .net 中返回结果 库,因为我已经使用测试项目来验证 API)。
我使用过的源码如下:
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 应用程序代码:
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;
如果有人遇到类似问题并且能够解决它,那将是非常好的。
最佳答案
像下面这样调用 .Result 会导致死锁
var result = table.ReadAsync("$top=10", null, wrapResult: true).Result;
我对 MobileServicesClient.InvokeApiAsync() 调用有同样的问题
相反,您应该像这样等待:-
async Task ReadTable()
{
var result = await table.ReadAsync("$top=10", null, wrapResult: true);
// do something with result
}
或者以我为例
async Task CallApi()
{
var response = await App.client.InvokeApiAsync ("/api/call", System.Net.Http.HttpMethod.Get, null);
// do something with response
}
关于c# - Azure 移动服务客户端查询未将控制权返回给 Xamarin Form android 客户端应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32002803/