加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Azure移动服务客户端查询不将控制权返回给Xamarin表单Andr

发布时间:2020-12-15 22:18:30 所属栏目:百科 来源:网络整理
导读:我正在使用带有Xamarin Form Android应用程序的Azure Mobile Service来主要查询来自azure表存储的数据. 我面临的当前问题是azure移动服务客户端没有在移动服务客户端之后立即返回控制 API调用(仅适用于Portable类库和android app项目,但同一调用会返回正常.n
我正在使用带有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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读