任务<&WebResponse的GT;。结果总是空 [英] Task<WebResponse>.Result is always null

查看:181
本文介绍了任务<&WebResponse的GT;。结果总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一些code通过Task.Factory.FromAsync做一个Htt的prequest(在WP7应用程序)。

该任务的结果属性始终是空的,但我知道这个请求本身是正确的,因为如果我把它贴到我的浏览器或提琴手,它的工作原理。这是我的code:

 字符串_url =的String.Format(http://requestapi.net/{0}/{1}/{2},
            物,partitionKey,pkey1);
        VAR请求=(HttpWebRequest的)WebRequest.Create(_url);
        request.Method =GET;        任务<&WebResponse的GT;任务1 =任务<&WebResponse的GT; .Factory.FromAsync(
            (回调,0)=> ((HttpWebRequest的)O).BeginGetResponse(回调,邻)
            ,结果=> ((HttpWebRequest的)result.AsyncState).EndGetResponse(结果)
            ,请求);        task1.Start();
        WebResponse类WebResponse类= task1.Result;
        串responseString;        使用(VAR响应=(HttpWebResponse)WebResponse类)
        {
            使用(流streamResponse = response.GetResponseStream())
            {
                StreamReader的读者=新的StreamReader(streamResponse);
                responseString = reader.ReadToEnd();
                reader.Close();
            }
        }

更新:WP7上的第三方物流是唯一可通过的NuGet。我这里下载吧:
http://nuget.org/packages/System.Threading.Tasks

更新:
这工作。迈克是正确的 - 只是没有执行完成的任务。我不知道为什么task1.Result没有自动等待(它应该隐含调用task1.wait()),但这是工作code。请让我知道,如果你看到的其他问题与此!这code开头,其中task1.Start()曾经是 - 这是现在拆除。

  //task1.Start();
            串responseString;
            task1.ContinueWith((前身)=>
            {                WebResponse类WebResponse类= task1.Result;                使用(VAR响应=(HttpWebResponse)WebResponse类)
                {
                    使用(流streamResponse = response.GetResponseStream())
                    {
                        StreamReader的读者=新的StreamReader(streamResponse);
                        responseString = reader.ReadToEnd();
                        reader.Close();
                    }
                }
            });


解决方案

你基本上创建任务,而不是等待它检查结果之前完成。从评论,似乎WP7运行时和框架从完整的CLR,当你试图让其结果会阻止工作有点不同。 Silverlight运行时是非常抗粘连-的东西。

您将要使用的await 关键词(这是可以在较新的运行时间或使用的异步瞄准包如果你有VS2012)或 ContinueWith 方法来异步启动任务。

I'm working on some code to do an HttpRequest via Task.Factory.FromAsync (in a WP7 app).

The task's Result property is always null, but I know the request itself is correct, because if I paste it into my browser or Fiddler, it works. This is my code:

        string _url = string.Format("http://requestapi.net/{0}/{1}/{2}", 
            "objects","partitionKey","pkey1");
        var request = (HttpWebRequest)WebRequest.Create(_url);
        request.Method = "GET";

        Task<WebResponse> task1 = Task<WebResponse>.Factory.FromAsync(
            (callback, o) => ((HttpWebRequest)o).BeginGetResponse(callback, o)
            , result => ((HttpWebRequest)result.AsyncState).EndGetResponse(result)
            , request);

        task1.Start();
        WebResponse webResponse = task1.Result;
        string responseString;

        using (var response = (HttpWebResponse)webResponse)
        {
            using (Stream streamResponse = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(streamResponse);
                responseString = reader.ReadToEnd();
                reader.Close();
            }
        }

UPDATE: on WP7, the TPL is only available via Nuget. I downloaded it here: http://nuget.org/packages/System.Threading.Tasks

UPDATE: This works. Mike was right - the task just wasn't finished executing. I'm not sure why task1.Result didn't wait automatically (it's supposed to implicitly call task1.wait()), but this is the working code. Please let me know if you see other problems with this! This code starts where the task1.Start() used to be - which is now removed.

            //task1.Start();
            string responseString;
            task1.ContinueWith((antecedent) =>
            {

                WebResponse webResponse = task1.Result;

                using (var response = (HttpWebResponse)webResponse)
                {
                    using (Stream streamResponse = response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(streamResponse);
                        responseString = reader.ReadToEnd();
                        reader.Close();
                    }
                }
            });

解决方案

You're basically creating a task, but not waiting for it to complete before checking the result. From the comments, it seems the WP7 runtime and framework work a bit differently from the full CLR, which would block when you tried to get the result. The Silverlight runtime is very anti-blocking-things.

You'll want to use the await keyword (which is available on newer runtimes or using the Async Targeting Pack if you have VS2012) or the ContinueWith method to start your task asynchronously.

这篇关于任务&LT;&WebResponse的GT;。结果总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆