我正在使用带有Xamarin Form
Android应用程序的Azure Mobile Service来主要查询来自azure表存储的数据.
我面临的当前问题是azure移动服务客户端没有在移动服务客户端之后立即返回控制
API调用(仅适用于Portable类库和android app项目,但同一调用会返回正常.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",wrapResult: true).Result;
如果有人有类似的问题并且能够解决它,那将是很好的.
解决方法
调用.Result如下所示会导致死锁
var result = table.ReadAsync("$top=10",wrapResult: true).Result;
我在MobileServicesClient.InvokeApiAsync()调用时遇到了同样的问题
相反,你应该等待: –
async Task ReadTable() { var result = await table.ReadAsync("$top=10",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 }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。