C# Closing a HttpWebRequest DevDefined

2019-07-10 04:55发布

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

0条回答
登录 后发表回答