0

Okay, I'm afraid I'm a bit lost on this one. The following code gives a CA 2000 warning that "'new WebRequestHandler()' is not disposed along all exception paths".

this.webHandler = new WebRequestHandler()
{
    AllowAutoRedirect = true,
    AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
    CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache),
    CookieContainer = cookies,
    ReadWriteTimeout = 5000
};

I struggled with that for a good 20 minutes before discovering that what I'm convinced is functionally the same code does not give that same warning.

this.webHandler = new WebRequestHandler();
this.webHandler.AllowAutoRedirect = true;
this.webHandler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
this.webHandler.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
this.webHandler.CookieContainer = cookies;
this.webHandler.ReadWriteTimeout = 5000;

Obviously, the solution is to use the latter approach (or just suppress the warning), but is there something wrong with the first approach that I'm just not aware of?

  • Possible duplicate of [using object initializer generates CA 2000 warning](http://stackoverflow.com/questions/8739065/using-object-initializer-generates-ca-2000-warning) –  Aug 15 '16 at 02:42

1 Answers1

0

Searching more broadly, I just found my answer: using object initializer generates CA 2000 warning

This one can be closed as a duplicate.

Community
  • 1
  • 1