简体   繁体   English

用于Metro应用程序前台背景锁HttpWebRequest的Windows 8 C#便携式库

[英]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. 我在都会应用程序的Windows 8 C#库上遇到问题。 I use an HttpWebRequest object to send datas and receive server response with BeginGetRequestStream method. 我使用HttpWebRequest对象使用BeginGetRequestStream方法发送数据并接收服务器响应。 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. 我发现此主题Windows 8.1 App-HttpWebRequest仅工作一次,但由于使用C#可移植库,因此无法使用“等待”。

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 在网络堆栈跟踪上,它被锁定在:退出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. 我不确定这是否是问题所在,但是Windows Phone中类似问题的常见原因是试图阻止UI线程上的网络结果,这将导致死锁。 I'm not sure if this happens in Windows Store apps, but it's worth checking for. 我不确定Windows Store应用程序中是否会发生这种情况,但是值得检查一下。
  • 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 . 根据您所针对的平台,默认情况下将支持它,但是如果不是,则可以使用Microsoft.Bcl.Async NuGet包获得它。
  • I'd recommend using HttpClient instead of HttpWebRequest. 我建议使用HttpClient而不是HttpWebRequest。 It's a nicer API that works well with async/await. 这是一个更好的API,可以很好地与async / await配合使用。 You can get it in PCLs with the Microsoft.Net.Http NuGet package . 您可以使用Microsoft.Net.Http NuGet包在PCL中获得它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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