与BeginGetRequestStream / EndGetRequestStream的TaskFactory.FromAsync挂起

时间:2012-12-31 06:25:55

标签: c# asynchronous windows-phone-8

我有一个这样的方法,在使用Task.Factory.FromAsync()

声明responseObject时挂起
private async Task<string> post(string url, string postdata)
        {
            var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            request.Method = "POST";

            // this works
            Task<Stream> requestStream = Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request);
            var sw = new StreamWriter(requestStream.Result);
            byte[] data = Encoding.UTF8.GetBytes(postdata);
            await requestStream.Result.WriteAsync(data, 0, data.Length);

            // this hangs
            Task<WebResponse> responseObject = Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request); // Hangs here
            // Doesn't get to here
            var responseStream = responseObject.Result.GetResponseStream();
            var sr = new StreamReader(responseStream);
            string received = await sr.ReadToEndAsync();

            return received;
}

如果我手动运行Begin / End方法,它可以正常工作,如下所示:

        request.BeginGetRequestStream(async ar =>
        {
            var requestStream = request.EndGetRequestStream(ar);
            using (var sw = new StreamWriter(requestStream))
            {
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                await requestStream.WriteAsync(data, 0, data.Length);
            }

            request.BeginGetResponse(async a =>
            {
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(a);
                var responseStream = response.GetResponseStream();
                using (var sr = new StreamReader(responseStream))
                {
                    string received = await sr.ReadToEndAsync();
                }

            }, null);
        }, null);

但是,在这种情况下,ReadToEndAsync()在一个单独的线程上运行,我希望该方法返回结果,这是因为该方法在BeginGetResponse回调完成之前返回,这是不可能的。

在这一点上,我认为我正在做这个可怕的错误并完全以错误的方式解决这个问题,所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:11)

首先:不要使用&#34; .Result&#34;在异步方法中。这将阻止运行方法的线程。使用&#34;等待&#34;相反,所以当获得结果时,线程会跳回到你的方法。

您的代码的问题在于您打开了请求流,但您从未关闭它。那么什么时候应该完成你的请求并发送它?它总是期望有更多的输入,直到请求在超时运行。您还没有设置内容类型和内容长度。

private async Task<string> post(string url, string postdata)
    {
        var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
        request.Method = "POST";

        byte[] data = Encoding.UTF8.GetBytes(postdata);
        request.ContentLength = data.Length;

        using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
        {
            await requestStream.WriteAsync(data, 0, data.Length);
        }


        WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
        var responseStream = responseObject.GetResponseStream();
        var sr = new StreamReader(responseStream);
        string received = await sr.ReadToEndAsync();

        return received;
}