httpclient.postasync()方法永远悬挂在C#Xunt测试中

发布于 2025-02-13 14:03:45 字数 610 浏览 0 评论 0 原文

我有600个功能性Xunit测试,每个测试都在调用 httpclient.getAsync(apiurl) httpclient.postasync(apiurl,data)它将称为一些外部API。

当我依次执行每个测试时,所有测试都可以正常工作。同样,即使我运行5-6个测试用例,也可以正常工作。

我的问题是,当我从顶部启动所有600个测试用例(将启动50个左右600个测试用例)时,所有测试都悬挂在发送 httpclient.postasync(apiurl,data)方法。

同样的问题发生在执行 httpclient.getAsync(apiurl)方法的点。但是,在我将其更改为 httpclient.getAsync(apiurl).configureawait(false)它解决了问题。

但是,当我更改 httpclient.postasync(apiurl,data) httpclient.postasync(apiurl,data).configureawait(false)问题仍然相同。

有人可以解释为什么会发生这种情况以及我该怎么办来解决这个问题。

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.

When I execute each test sequentially all working fine. Also even when I run 5 - 6 test cases parallelly then also everything works fine.

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.

The same issue happened at the point where it executes the httpClient.GetAsync(apiUrl) method. But after I change it as httpClient.GetAsync(apiUrl).ConfigureAwait(false) it fix the issue.

But when I change the httpClient.PostAsync(apiUrl, data) to httpClient.PostAsync(apiUrl, data).ConfigureAwait(false) issue is still the same.

Can someone explain why this happens and what should I do to fix this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2025-02-20 14:03:46

当我更改 httpclient.postasync(apiurl,data)方法下面时,它将解决该问题。我不确定两种方法之间有什么区别。

以前,

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

我在下面修改了上面的修改,并解决了问题。

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

我还发现以下帖子已经讨论过一些类似的问题。如果有人面临类似问题,请遵循以下帖子,因为它包含一些更有用的提示和原因。

When I change the httpClient.PostAsync(apiUrl, data) method as below, it fix the issue. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文