简体   繁体   English

HttpClient.PostAsync() 方法在 C# xUnt 测试中永远挂起

[英]HttpClient.PostAsync() method is hanging forever in C# xUnt test

I have 600 functional xUnit tests and each of the test is calling a httpClient.GetAsync(apiUrl) and httpClient.PostAsync(apiUrl, data) methods in the middle of the test which will call some external APIs.我有 600 个功能性 xUnit 测试,每个测试都在测试中间调用httpClient.GetAsync(apiUrl)httpClient.PostAsync(apiUrl, data)方法,这将调用一些外部 API。

When I execute each test sequentially all working fine.当我按顺序执行每个测试时,一切正常。 Also even when I run 5 - 6 test cases parallelly then also everything works fine.此外,即使我并行运行 5 - 6 个测试用例,也一切正常。

My problem is, when I start all 600 test cases from the top (which will start around 50 out 600 test cases parallelly), all the tests are hanging where it sends the httpClient.PostAsync(apiUrl, data) method.我的问题是,当我从顶部启动所有 600 个测试用例时(将并行启动 600 个测试用例中的 50 个左右),所有测试都挂在它发送httpClient.PostAsync(apiUrl, data)方法的地方。

The same issue happened at the point where it executes the httpClient.GetAsync(apiUrl) method.同样的问题发生在它执行httpClient.GetAsync(apiUrl)方法的地方。 But after I change it as httpClient.GetAsync(apiUrl).ConfigureAwait(false) it fix the issue.但是在我将它更改为httpClient.GetAsync(apiUrl).ConfigureAwait(false)后,它解决了这个问题。

But when I change the httpClient.PostAsync(apiUrl, data) to httpClient.PostAsync(apiUrl, data).ConfigureAwait(false) issue is still the same.但是当我将httpClient.PostAsync(apiUrl, data)更改为 httpClient.PostAsync(apiUrl, data) httpClient.PostAsync(apiUrl, data).ConfigureAwait(false)问题仍然相同。

Can someone explain why this happens and what should I do to fix this issue.有人可以解释为什么会发生这种情况以及我应该怎么做才能解决这个问题。

When I change the httpClient.PostAsync(apiUrl, data) method as below, it fix the issue.当我如下更改httpClient.PostAsync(apiUrl, data)方法时,它可以解决问题。 I'm not sure what the difference between two methods.我不确定这两种方法有什么区别。

Previous way以前的方式

var response = await httpClient.PostAsync(apiUrl, data);

I modified above as below and it fixed the issue.我在上面进行了如下修改,它解决了这个问题。

var task = httpClient.PostAsync(apiUrl, data);
var response = task.GetAwaiter().GetResult();

Also I found some similar problem that has been discussed in below post.我还发现了一些类似的问题,已在下面的帖子中讨论过。 If someone facing similar issue, please follow below post as it contains some more useful tips and reasons.如果有人遇到类似问题,请关注下面的帖子,因为它包含一些更有用的提示和原因。 HttpClient.GetAsync(...) never returns when using await/async HttpClient.GetAsync(...) 在使用 await/async 时永远不会返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 httpClient.PostAsync(URL,content)在C#Metro应用中不起作用 - httpClient.PostAsync(url, content) not working in C# Metro apps C# HttpClient.PostAsync 不返回或抛出错误 - C# HttpClient.PostAsync doesn't return or throw an error HttpClient.PostAsync Xamarin.Android C#错误响应 - HttpClient.PostAsync Xamarin.Android C# wrong response C#HttpClient.PostAsync只工作一次 - c# HttpClient.PostAsync only works once C# HttpClient PostAsync 永远阻塞 - C# HttpClient PostAsync blocks forever HttpClient.PostAsync挂起,直到启动所有System.Timers - HttpClient.PostAsync Hanging Until All System.Timers Are Started c# 单元测试 httpclient postasync - c# unit test httpclient postasync 我在 HttpClient.PostAsync (C#) 中收到 StatusCode: 401 “Unauthorized” - I am getting StatusCode: 401 “Unauthorized” in a HttpClient.PostAsync (C#) 为什么我的 C# winforms 应用程序中的第一个 HttpClient.PostAsync 调用非常慢? - Why is first HttpClient.PostAsync call extremely slow in my C# winforms app? httpClient.PostAsync返回“不允许使用方法”(在winform应用程序中运行) - httpClient.PostAsync returns “Method Not Allowed” (running in winform app)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM