C# 关闭 HttpWebRequest DevDefined

发布于 2024-11-28 14:59:16 字数 1356 浏览 1 评论 0原文

我在使用 C# 的 DevDefined OAuth 库连接到第 3 方 API 时遇到问题:

https:// github.com/bittercoder/DevDefined.OAuth

该连接已经工作正常一年多了,但在过去的两天里,每隔几个小时就开始失败。异常消息是:

The request was aborted: Could not create SSL/TLS secure channel

应用程序池重新启动后它可以正常工作,但几个小时后停止进行身份验证 - 这让我认为它没有关闭 HttpWebRequest,并且我们正在泄漏连接并最终耗尽。

类似的问题表明 KeepAlive 可能会解决这个问题,但它对我没有帮助: ASP.NET - 请求被中止:无法创建 SSL/TLS 安全通道

但代码相当复杂,并且 HttpWebRequest 对象在创建后会传递给许多其他函数。我看不到在哪里关闭它。而且,我不知道如何关闭 HttpWebRequest,所以我的问题是:

  • 如何找到关闭此 HttpWebRequest 的位置?
  • 如何关闭 HttpWebRequest?或者是必须处理的响应?

DevDefined 代码如下所示:

public virtual HttpWebRequest ToWebRequest()
{
    var request = (HttpWebRequest) WebRequest.Create(description.Url);
    // .. snip setting request properties
    return request;
}

在多个位置调用函数 ToWebRequest(),从这些位置提取 Response 并将其返回到更多方法。因此事实证明很难追踪它。我确实发现这个方法似乎表明响应正在使用但未处理:

public override string ToString()
{
    if (string.IsNullOrEmpty(ResponseBody))
    {
    ResponseBody = ToWebResponse().ReadToEnd();
    }
    return ResponseBody;
}

I'm having a problem connecting to a 3rd party API using the DevDefined OAuth library for C#:

https://github.com/bittercoder/DevDefined.OAuth

The connection has been working fine for over a year now, but in the last two days it has started failing every few hours. The exception message is:

The request was aborted: Could not create SSL/TLS secure channel

It works as normal after the app pool has been restarted but then stops authenticating after a few hours - this makes me think that it is not closing the HttpWebRequest, and we are leaking connections and eventually running out.

A similar question suggests that KeepAlive might solve this, but it did not help me: ASP.NET - The request was aborted: Could not create SSL/TLS secure channel

But the code is quite complex, and the HttpWebRequest object is passed around to many other functions after being created. I can't see where to close it. And also, I don't know how to close a HttpWebRequest, so my question is:

  • How can I find where to close this HttpWebRequest?
  • How do you close a HttpWebRequest? Or is it the Response that must be disposed?

The DevDefined code looks like this:

public virtual HttpWebRequest ToWebRequest()
{
    var request = (HttpWebRequest) WebRequest.Create(description.Url);
    // .. snip setting request properties
    return request;
}

The function ToWebRequest() is called in several places, and from those places the Response is extracted and returned to even more methods. So its proving to be difficult to track it down. I did find this method that seems to show that the Response is being used but not disposed:

public override string ToString()
{
    if (string.IsNullOrEmpty(ResponseBody))
    {
    ResponseBody = ToWebResponse().ReadToEnd();
    }
    return ResponseBody;
}

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

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

发布评论

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