Adrian
Adrian

Reputation: 719

HttpRequestException using HttpClient at the activated event in Windows Phone

I noticed that when I press the Windows button while doing a network request, and then, press the back button to come back to the app, the process is killed (HttpRequestException is thrown). I know that this happens because the Operating Sytem closes all networking connections when the app goes to the dormant state.

How can I handle properly this scenario? I mean, if I catch the exception, I should retry the network operations. I read that I could use a bool property that is set in the activated event to retry operations, but I don't like this approach because is easy that it fails.

Upvotes: 2

Views: 430

Answers (2)

Neil Turner
Neil Turner

Reputation: 2727

This article from 2012 gives a good overview of what happens to a WebClient instance when the app is suspended or tombstoned, and some solutions...

http://community.appamundi.com/blogs/andywigley/archive/2012/04/08/what-happens-to-network-calls-when-your-wp7-app-goes-dormant.aspx

As HttpClient is a newer API, which supports Async/Await, another suggestion would be to use one of the RetryOnFault patterns from the Consuming the Task-based Asynchronous Pattern page on MSDN.

Upvotes: 1

DotNetRussell
DotNetRussell

Reputation: 9857

Register to

 PhoneApplicationService.Current.Closing

It will be fired when your app is closing. There you can handle stopping any processes you need to.

Upvotes: 0

Related Questions