在Net3.5死锁中调用异步方法

时间:2017-04-21 09:17:21

标签: c# async-await .net-3.5 deadlock configureawait

我正在使用.net3.5,我安装了以下内容 enter image description here 此操作冻结。谁能告诉我我应该采取哪些不同的做法?

HttpClient Client;
    response =  await  Client.SendAsync(request, cancellationToken).ConfigureAwait(false);

之前我在Net4.0中安装了我的项目,并且相同的代码行使用了以下库。

enter image description here

我需要降级到Net3.5,现在我的异步调用挂起,响应状态为WaitingForActivation

我也意识到以下工作:

var response = await _client.GetAsync(Url, cancellationToken).ConfigureAwait(false);

还在response.Results中返回一个值。也许是库中的SendAsync会发生什么?

经过进一步调查后我意识到以下

        var request1 = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost:7000/connect/token"));
        var request2 = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost:7000/connect/token"))
        {
            Content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("grant_type", "client_credentials"),
                new KeyValuePair<string, string>("scope", "hq_api_access")
            })

        };

        var response1 = await client.SendAsync(request1).ConfigureAwait(false);
        var response2 = await client.SendAsync(request2).ConfigureAwait(false);

request2挂起。我猜它是一个在使用内容设置请求对象时发生的库中的错误。我可以使用另一个客户吗?对于.net 3.5平台,如果没有打捞这个。

最小,完整且可验证的示例 正如下面的评论所示。我创建了一个示例来重新编写问题。我正在使用Visual Studio 2015.创建了一个新的.net3.5控制台应用程序。

我安装了以下内容  1.安装包Rackspace.HttpClient35 -Pre  2.安装包TunnelVisionLabs.Threading -Pre

以下是主程序的外观

class Program
    {
        static void Main(string[] args)
        {
            var r = Demo();
            var a = r.Result;
        }

        public static async Task<HttpResponseMessage> Demo()
        {
            var client = new HttpClient();
            var request1 = new HttpRequestMessage(HttpMethod.Post,
                new Uri("https://demo.identityserver.io/connect/token"));
            var request2 = new HttpRequestMessage(HttpMethod.Post,
                new Uri("https://demo.identityserver.io/connect/token"))
            {
                Content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("grant_type", "client_credentials"),
                    new KeyValuePair<string, string>("scope", "api")
                })
            };

            var response1 = await client.SendAsync(request1).ConfigureAwait(false);
            var response2 = await client.SendAsync(request2).ConfigureAwait(false);

            return response1;
        }
    }

当我使用breakpionts进行调试时,response1会返回响应,但response2会挂起。

enter image description here

0 个答案:

没有答案
相关问题