News for hackers. Stuff that matters.
Re:Using the .NET HttpClient class wrongly can destabilize s... by dogma1138 on Tuesday August 30, 2016 @03:08PM (#12392956)
To be fair this isn't using the HttpClient incorrectly this is using "OOP" at least .NET incorrectly.

"Do not initialize unnecessarily" is a pretty common paradigm in .NET this extends way beyond variables and constructors. Even if the HttpClient did not cause this http.sys/winsock errata there is still absolutely no reason to initialize a new instance of it within a loop you are wasting CPU time and more importantly memory and leaving your garbage collector extra work.

Idealy a loop should not really initialize or create any new objects, create what you need an access it from within the loop, initializing a new instance of a class in every cycle of the loop, creating new variables, subscribing to event handlers etc. is just really really bad coding practice and this is basic .NET performance practices.

The "lesson" the author should've learned isn't that HttpClient is designed oddly, or that there are some oddities with how Windows handles sockets, but that "var foo = new bar()" and the likes do not belong inside a loop or any other iterative statement unless there is absolutly no other way of doing without it (and I can't think of a scenario in which that would be the case).

Re:Using the .NET HttpClient class wrongly can destabilize s... by artpepper on Tuesday August 30, 2016 @04:43PM (#12393723)
I think the case that trips people up is using HttpClient in an ASP.NET MVC controller action. There, it doesn't look like a loop, but if you create/dispose on each request, you can get socket exhaustion under load.
Re:Using the .NET HttpClient class wrongly can destabilize s... by dogma1138 on Tuesday August 30, 2016 @05:26PM (#12394052)
If that's the case you should use HTTP pipelining usually to bundle multiple requests via a single tcp connection.

https://en.m.wikipedia.org/wiki/HTTP_pipelining

Have it sitting in the background waiting for the controller to ask it to do something, you can manage the sockets and you can make a request queue on your end if you getting into resource cap issues.