C#httpclient taskCanceLedException和socketException

发布于 2025-01-30 16:13:51 字数 719 浏览 1 评论 0原文

我们有一个旧的系统,其中有很多代码类似:

using(var httpClient = new HttpClient()){...}

目前我们无法更新代码,因为这需要我们的老板和客户的批准。

最近,我们添加了一些功能,并且我们正在为DI使用Unity,并在DI容器中添加了HTTPClient作为Singleton

container.RegisterSingleton<HttpClient>();

,并且我们正在为服务中注入实例。 但是,在我们部署更改后,我们将遇到的错误:

  1. 发生一个或多个错误。一个任务被取消。
  2. SocketException随着服务器关闭了预期将保持活力的连接消息的消息。 ....

在网上阅读了一些问题后,我发现了 httpclient-取消了任务?

处置HTTPClient实例可能会导致http请求之后的httpclient的其他实例启动!

这是否意味着,使用()语句将实例放在DI容器中?

We have an old system with a lot of code similar below:

using(var httpClient = new HttpClient()){...}

We aren't able to update the code at the moment because that will require some approval from our boss and our client.

Recently we added some feature and we are using Unity for DI and we added the HttpClient as singleton in our DI Container

container.RegisterSingleton<HttpClient>();

and we are injecting the instance in our service.
But after we deploy the changes we are getting below errors:

  1. One or more errors occurred. A task was canceled.
  2. SocketException with the message of A connection that was expected to be kept alive was closed by the server. ....

After reading some questions online, I found this HttpClient - A task was cancelled?

Disposing the HttpClient instance can cause following HTTP requests started by other instances of HttpClient to be cancelled!

Does it mean, the using() statements was disposing the instance in DI container?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文