TaskFactory.FromAsync with BeginGetRequestStream/EndGetReque

本文关键字:EndGetReque BeginGetRequestStream with FromAsync TaskFactory | 更新日期: 2023-09-27 17:49:59

我有一个这样的方法,当使用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回调完成之前返回。

在这一点上,我认为我做得非常错误,并且完全错误的方式,所以任何帮助都将非常感激。

TaskFactory.FromAsync with BeginGetRequestStream/EndGetReque

首先:不要使用"。Result"在异步方法中。这将阻塞正在运行方法的线程。使用"await"代替,这样当获得结果时,线程就会跳转回你的方法。

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

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;
}