0

When using https url i am facing memory leaks and with http protocol it works fine . I ran profiler here is the result . I tried every thing but no use memory keeps increasing with every request .

Memory Profiler Image

 public class HttpService<T> where T : class
    {
        public static T GetResponse(string url, string data, string username = null, string password = null, Int64 ConId = 0, ControllerModel cont = null, string authType = "Digest", bool _isCameraServer = false, [CallerLineNumber] long callerLineNumber = 0, [CallerFilePath] string callerFilePath = "")
        {
           // url = url.ToLower();
           //url = url.Replace("https", "http");
            string pageContent = "";
            try
            {
                Uri myUri = new Uri(url);
                #region For HTTPS
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                       | SecurityProtocolType.Tls11
                       | SecurityProtocolType.Tls12
                       | SecurityProtocolType.Ssl3;
                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                #endregion
                if (_isCameraServer)
                {
                    ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
                }
                using (WebClient cli = new WebClient())
                {
                    cli.Headers.Add("Cache-Control", "no-cache");
                    cli.Headers.Add("Cache-Control", "no-store");
                    cli.Headers.Add("User-Agent", "Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36");
                    

                    cli.Headers[HttpRequestHeader.ContentType] = "application/json";                         
                    cli.Credentials = new NetworkCredential(username, password);
                    cli.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);                    
                    string res = cli.UploadString(url,"POST", data);
                var ss = JsonConvert.DeserializeObject<T>(res);
                    res = null;
                return ss;
                }

            }
            catch (StackOverflowException ex)
            {
                Helper.Log(ex);
                return null;
            }
            catch (Exception ex)
            {
                Helper.Log(ex);            
                return null;
            }
        }
    }
BasitNoor
  • 5
  • 2
  • Why are you using WebClient? It’s deprecated for almost 10 years now and Microsoft recommends using HttpClient. Also it’s generally useless to catch a StackOverflowException. If your code can achieve such a state, then your design is wrong, and even when caught it’s unlikely this will succeed as there is no stack available where you could put your `ex` parameter on to call Helper.Log. – ckuri Apr 22 '21 at 21:16
  • Thanks for your observation . I tried using Web Request / Http Client now testing Web Client but the problem is https with ssl that is causing memory to grow over the certain period of time until it crashes with out of memory exception . Also i have used allowWriteBuffering = false and AllowRedirect to false as most people have recomended over the net – BasitNoor Apr 23 '21 at 06:32
  • @ckuri also see this regarding webclient https://stackoverflow.com/questions/20530152/deciding-between-httpclient-and-webclient – BasitNoor Apr 23 '21 at 09:23

0 Answers0