C# Closing a HttpWebRequest DevDefined

时间秒杀一切 提交于 2019-12-13 15:35:05

问题


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;
}

来源:https://stackoverflow.com/questions/6993641/c-sharp-closing-a-httpwebrequest-devdefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!