C# Closing a HttpWebRequest DevDefined
Asked Answered
Q

0

2

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;
}
Quaquaversal answered 9/8, 2011 at 8:54 Comment(6)
Try creating a system.net trace log for your application ( ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html ). It should show you why the SSL handshake is failing.Acaroid
@Acaroid - thanks I got tracing working. But is is a massive log - is it possible to only log the connection attempt? the log is massive on my local dev box - it's going to be real bad on our live server with 1000's of users.Quaquaversal
How big is the file? Can you just grep through it for the SSL handshake failure?Acaroid
1,000's of users each making hundreds of requests an hour. Its a big file. But yes I did just search for the failure, and found the cause of the error: #6233246 and I've put a fix in place, now just waiting to see if it fails again.Quaquaversal
Was this problem ever resolved? - I have a similar problem connecting to Twitter from Amazon EC2 instances.Casta
@Lee we solved it by making sure our app worked in 64 bit compilation mode and setting IIS allow 32 bit apps = false.Quaquaversal

© 2022 - 2024 — McMap. All rights reserved.