0

In my windows phone 8 app i am using asynchronous methods to retrieve data from server.

After implementing Fast App Resume functionality another problem rose for me. The asynchronous method that retrieves data from server throws the exception of type System.Net.WebException when it resumes.

The steps to reproduce the problem is you just hit the start button when the app is loading data by asynchronous method.

For example i have a page that loads notification of user. I called the async void GetNotifications() method that further calls below method to retrieve response string.

    public async Task<string> Get(string URL)
    {
        var request = WebRequest.Create(new Uri(URL)) as HttpWebRequest;

        if (APIHelper.APIHandshake != null)
            request.Headers["Cookie"] = "ii=" + APIHelper.APIHandshake.Sid + ";";

        return await httpRequest(request);
    } 

the implementation of httprequest method is given below.

    private async Task<string> httpRequest(HttpWebRequest request)
    {
        string received;

        using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory
            .FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
        {
            using (var responseStream = response.GetResponseStream())
            {
                using (var sr = new StreamReader(responseStream))
                {
                    //cookieJar = request.CookieContainer;
                    //responseCookies = response.Cookies;
                    received = await sr.ReadToEndAsync();
                }
            }
        }

        return received.Replace("[{}]", "[]")
                .Replace("[{},{}]", "[]")
                .Replace("\"subcat_id\":\"\"", "\"subcat_id\":\"0\"");
    }

The user just click the menu that opens the notification page and then press the start button of phone instantly to deactivate the app. When user will click on application tile from start menu the exception will be thrown.

Any solution? will deactivating idle mode detection work?

LojiSmith
  • 282
  • 3
  • 20

1 Answers1

0

It would probably not be the best solution but It is possible to not cancel the async request by adding this line of code in the InitalizePhoneApplication() method on your app.xaml.cs page.

PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;

Read more on this property here
Test it, it should fix the issue but I'm not pretending that's the best thing to do....

Paul Martinez
  • 562
  • 1
  • 9
  • 19