...
/Consuming Web Services Using HTTP Clients and Getting Consumers
Consuming Web Services Using HTTP Clients and Getting Consumers
Learn about HttpClient and configuring HTTP clients.
Now that we have built and tested our Northwind service, we will learn how to call it from any .NET app using the HttpClient
class and its factory.
Understanding HttpClient
The easiest way to consume a web service is to use the HttpClient
class. However, many people use it wrongly because it implements IDisposable
, and Microsoft’s own documentation shows poor usage of it.
Usually, when a type implements IDisposable
, we should create it inside a using
statement to ensure it is disposed of as soon as possible. HttpClient
is different because it is shared, reentrant, and partially thread-safe.
The problem has to do with how the underlying network sockets have to be managed. The bottom line is that we should use a single instance of it for ...