简体   繁体   中英

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.

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 .

Register to

 PhoneApplicationService.Current.Closing

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM