TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sachin Singh
7
55.8k
82.8k
Best way to instantiate a HttpClient in winform and webforms.
Sep 13 2020 9:41 AM
As per MSDN
HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly.
public
class
GoodController : ApiController
{
private
static
readonly HttpClient HttpClient;
static
GoodController()
{
HttpClient =
new
HttpClient();
}
}
But in windows form (obviously controller doen't make sense here) or Webform we do not have controller , however in webform we can store instance of HttpClient in the Application property to use a single instance in all requests.
Application.Add(
"_Client"
,
new
HttpClient());
The other approach can be creating a singleton class like
public
class
ClientInstantiator
{
// OK
private
static
readonly HttpClient _Client;
static
ClientInstantiator
()
{
_Client =
new
HttpClient();
}
}
But in winform nothing make sense.
Am i over analyzing things?? as msdn also says that almost all useful async methods of HttpClient are thread safe like
CancelPendingRequests
DeleteAsync
GetAsync
GetByteArrayAsync
GetStreamAsync
GetStringAsync
PostAsync
PutAsync
SendAsync
Reply
Answers (
0
)
How can insert a datagridview combobox values in to database table in
Entry point not found exception