简体   繁体   中英

Windows 8 C# portable library for metro app foreground background lock HttpWebRequest

I have an issue on Windows 8 C# library for a metro application. I use an HttpWebRequest object to send datas and receive server response with BeginGetRequestStream method. But when I go background foreground several times I never reaching this code :

 HttpWebRequest req = HttpWebRequest.CreateHttp(monUri); req.BeginGetRequestStream(result => { /* My Code */ },req); 

I found this topic Windows 8.1 App - HttpWebRequest working only once but I cannot use "await" because I am in a C# portable library.

Moreover I have try to abort all connexion but when I start a new one I am already locked.

On network stacktrace it is locked at : Exiting HttpWebRequest#44973269::BeginGetRequestStream() -> ContextAwareResult#52835420

Several things:

  • I'm not sure if it's the problem here, but a common cause for something like this in Windows Phone is trying to block on a network result on the UI thread, which will deadlock. I'm not sure if this happens in Windows Store apps, but it's worth checking for.
  • Portable Class Libraries do support async and await. Depending on what platforms you are targeting it will be supported by default, but if it's not you can get it with the Microsoft.Bcl.Async NuGet package .
  • I'd recommend using HttpClient instead of HttpWebRequest. It's a nicer API that works well with async/await. You can get it in PCLs with the Microsoft.Net.Http NuGet package .

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