0

I'm developing an Web ASP.Net API to extend our current desktop aplication. Allong with the implementation of token based authentication, i had some problem with thread exceptions and while looking for a solution some doubts came to my mind. So using HttpClient when should it be a static initialized on first request or created each time the client access the API. I supose there's not a straight answer, but i would like your opinion.

Thanks

Vitor Silva
  • 161
  • 1
  • 1
  • 11

1 Answers1

0

EDIT

Apparently the MSDN is not up-to-date and the HttpClient does support multi-threading. Take a look to the comments for more details.


Short answer

Create the httpclient each time the client access the API.

Not so long answer

According to the MSDN:

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Creating a new instance at each request will ensure you will never experience any multithread troubles. It is may be not efficient in terms of memory footprint, but it is safe.

If you really know what you are doing (if you are certain only one thread will call your httpclient at the same time), you can create a shared client. To do so, you can take a look to semaphore for example.

fharreau
  • 2,105
  • 1
  • 23
  • 46
  • No, this is wrong. `HttpClient` is designed to be shared for many concurrent requests. See the article I linked to above, the question of which this is a duplicate, and the answers to this one: http://stackoverflow.com/q/11178220/240733. Also, now that we have async/await and the TPL, the need to use lower-level thread synchronization constructs such as semaphores has diminished. I would very rarely recommend this to anyone these days. (The people that know how to use it properly are those least likely to need the recommendation.) – stakx - no longer contributing Apr 24 '17 at 10:27
  • `No, this is wrong. HttpClient is designed to be shared for many concurrent requests` > Well, that's not what the MSDN said, but that would not be the first time the MSDN fools me :D. Anyway, the OP gets thread exceptions, so who to trust? You or the MSDN? – fharreau Apr 24 '17 at 10:43
  • Without seeing the OP's code that causes the exceptions, it is hard to say what is going wrong. But note that it is not me against MSDN; it's even people from Microsoft (from the Patterns and Practices group) who say that `HttpClient` should be shared. – stakx - no longer contributing Apr 24 '17 at 10:54
  • Yup, I agree with you. Is it possible that the HttpClient used to be not threadsafe in former versions of .NET and they did not update the MSDN with new versions? And may be the OP is using an old version of .NET? – fharreau Apr 24 '17 at 11:05
  • Thanks stakx the link you sent its just it. – Vitor Silva Apr 24 '17 at 12:26
  • @fharreau: The MSDN documentation is indeed weird. https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx is quite specific about the thread-safety of `HttpClient` (see Remarks section), while https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx (no version number in URL) does not have that information at all. – stakx - no longer contributing Apr 24 '17 at 16:01