ASP.NET Monsters #62: You are probably using HttpClient wrong

Play ASP.NET Monsters #62: You are probably using HttpClient wrong

Description

Retrieving data using the HttpClient is a fairly common practice in any number of applications, but HttpClient can easily be used incorrectly frequently with dire consequences. Make sure you're using it correctly in the latest ASP.NET Monsters video. 

Here are some references and other interesting reads:

 

Tags:

ASP.NET, Web

Embed

Download

The Discussion

  • User profile image
    afaz

    I have to say, as a British viewer, Simon should really drop the fake accent, it's very Dick Van * in the annoying/insulting scale, where as I'm sure his real voice is fine.

  • User profile image
    afaz

    Apparently the word "d y k e" is filtered here...

  • User profile image
    Canadian​James

    @afaz:As someone who's known Simon for over five years, you should really know that he's never once not had an accent. If it's fake, he's got even his wife fooled (she'll tell us the moment he slips).

  • User profile image
    DArcy Lussier

    Ha! Jokes on you afaz, that's how everyone talks in Calgary!

    :P

  • User profile image
    dragansr

    Did you try using HttpWebRequest instead of HttpClient?
    That is a lower level API that has more options, may be able to configure unconditional closing of socket when it is disposed.

  • User profile image
    Jarred​Capellman

    Great deep dive - you wouldn't have inferred HttpClient keeping sockets open

  • User profile image
    Trevor Dennis

    There is nothing wrong with TIME_WAIT connections. These are CLOSED connections and they are supposed to be there for a few minutes after a TCP connection closes. Their purpose is to make sure another program doesn't get that socket port after your app finishes with it because of those stray packets. The stray packets could screw up the new application.

    With your solution, you might not be able to multi-thread your requests without using locking which could slow down your application. In your example case you still call it sequentially and wait for ".Result" so you are not multi-threading despite calling GetAsync.

    Since you're not seeing multiple source ports anymore and the TIME_WAITs, I'd say you're not multi-tasking the requests at all. TCP cannot handle two conversations from the same source port at the same time.

    What is happening is the same thing that a browser would do and send the 10 multiple HTTP requests sequentially over the same one TCP connection to the web server. A web server allows these multiple requests on one connection in order to gain efficiency and reduce those separate connections, especially if SSL/TLS is used. The downside is loss of multi-tasking from your app.


  • User profile image
    Trevor Dennis

    Also, if you look closely at the netstat after your fix (at 10:34 in the video), you will see "aspnetmonsters.com [23.101.203.214]" as ESTABLISHED still, not TIME_WAIT.

    So, instead of a closed connection waiting in TIME_WAIT state which is good, you have a stale ESTABLISHED connection after your program has exited...

  • User profile image
    afaz

    @CanadianJames, I'm sorry but it even says on your site description "...and another one sports a fake Brittish (sic) accent..." I'm not trying to be a jerk here but you can hear him code-switching (in terms of accent)

  • User profile image
    Brian Conneen

    I love how a post about a .NET library turned into an "Accent Truther" debate.

    I was born and raised in the Philadelphia metro area and have been known to pronounce the word "water" as both "WA-TER" and "WOOD-ER". Among a long list of other area specific pronunciations I drop and pickup as I see fit.

    I never knew this was "code-switching" and thought of it more as, "I don't give a care".

  • User profile image
    Joe

    Thanks for the video! What is the name of that powershell client you are using?

  • User profile image
    wuhuangjia

    Thanks a lot to share it, guys ^^

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.