C# 在Net3.5死锁中调用异步方法
我使用的是.net3.5,我安装了以下软件 此操作冻结。有谁能告诉我我应该做些什么不同的事情吗C# 在Net3.5死锁中调用异步方法,c#,async-await,.net-3.5,deadlock,configureawait,C#,Async Await,.net 3.5,Deadlock,Configureawait,我使用的是.net3.5,我安装了以下软件 此操作冻结。有谁能告诉我我应该做些什么不同的事情吗 HttpClient Client; response = await Client.SendAsync(request, cancellationToken).ConfigureAwait(false); 我以前在Net4.0中有过我的项目,同样的代码行用于以下库 我需要降级到Net3.5,现在我的异步调用挂起,响应状态为WaitingForActivation 我也刚刚意识到以下方
HttpClient Client;
response = await Client.SendAsync(request, cancellationToken).ConfigureAwait(false);
我以前在Net4.0中有过我的项目,同样的代码行用于以下库
我需要降级到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);
使用breakpionts进行调试时,response1返回一个响应,但response2挂起
您是否在程序中的任何位置对任务执行
.Result
或.Wait()
?@LasseV.Karlsen是,但其响应为空。结果为空。不,如果调用了Task.Result
,我没有使用.wait(),很可能就是死锁所在的地方。@PauloMorgado检查我的更新。死锁仅在我设置HttpRequestMessage对象的内容时发生。否则我会得到回应。你能提供一个答案吗?
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;
}
}