Azure移动服务调用挂起

时间:2017-04-18 14:01:42

标签: xamarin.android xamarin.forms azure-mobile-services

使用Xamarin.Forms应用。当移动数据连接在Android设备上波动时,例如网络从3G到2G连接,反之亦然 - 这个电话好像挂在某处。

这是Azure API调用:

public Task<ObservableCollection<Models.Item>> GetItemsByID(string tenantID, string auth_token)
    {
        return Task.Factory.StartNew(() =>
        {
            var res = RestClient.Get<ObservableCollection<Models.Item>>(HttpWebRequest.Create(string.Format(EndPointsList.GetItemsUrl + "tenantID={0}", tenantID)),auth_token);
            return res ;
        });
    }

我们在这里启动System.Net.WebRequest:

        public static T Get<T>(WebRequest request, string auth_token, string requestData = null)
    {
        string result = string.Empty;

        request.ContentType = "application/json";
        request.Headers["ZUMO-API-VERSION"] = "2.0.0";

        if (auth_token.StartsWith("Bearer"))
            request.Headers["Authorization"] = auth_token;
        else {
            request.Headers["x-access_type"] = "offline";
            request.Headers["x-zumo-auth"] = auth_token;
        }

        try
        {
            WebResponse webResponse = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null).Result;
            using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
            var typ = typeof(T);
            if (
                typ == typeof(String)
                || typ == typeof(float)
                || typ == typeof(Decimal)
                || typ == typeof(Int16)
                || typ == typeof(Int32)
                || typ == typeof(Int64)
            )
            {
                return (T)Convert.ChangeType(result, typeof(T), null);
            }
            return result.FromJson<T>();

        }

        catch (AggregateException agEx)
        {
            AggregateException(agEx);
            return result.FromJson<T>();
        }
        catch (Exception ex)
        {
            return result.FromJson<T>();
        }
    }

这是来自ViewModel的调用

    public async Task GetAllItems()
    {
        try
        {
            if (!this.IsInternetConnectionAvailable())
            {
                await this.CurrentContentPage.DisplayAlert("", AppResources.InternetConnectionNotAvailable, AppResources.Ok);
                return;
            }
            this.ProgressBar.ShowProgress(AppResources.Loading);

            ItemList = await this.ItemService.GetItemsByID(App.Locator.Login.LoggedInUser.TenantID.ToString(),Settings.AuthToken);
            if (ItemList != null)
            {
                for (int i = 0; i < this.ItemList.Count; i++)
                {
                    ItemList[i].RowColor = (i % 2 == 0 ? Theme.EvenRowColor : Theme.OddRowColor);
                }
            }
            RaisePropertyChanged("ItemList");
        }
        catch (Exception ex)
        {
            ExceptionHandler.HandleException(CurrentContentPage, ex);
        }
        finally
        {
            this.ProgressBar.Dismiss();                
        }
    }

在这里的实际情况中 - ProgressBar将继续显示在设备上,尽管它在Finally块中。用户必须终止应用程序以使其再次运行。 而且我们在实验室中重现这一点并不成功。它只发生在间歇连接的现场。

代码有异常吗?如果没有,我们如何在应用程序日志中捕获它。

0 个答案:

没有答案