.NET

HttpClient & Networking

HttpClient, HttpClientFactory, request/response, error handling, retry policies, async patterns

20 câu hỏi phỏng vấn·
Mid-Level
1

Why use IHttpClientFactory instead of instantiating HttpClient directly?

Câu trả lời

IHttpClientFactory automatically manages the lifecycle of HttpClient instances and prevents socket exhaustion. Directly instantiating HttpClient in a using statement can cause TIME_WAIT connection issues that remain open even after disposal. Using IHttpClientFactory also enables configuring resilience policies with Polly and efficiently managing connection pooling.

2

What is the main difference between a Named Client and a Typed Client?

Câu trả lời

A Typed Client encapsulates HTTP call logic in a dedicated class with dependency injection, while a Named Client is retrieved via IHttpClientFactory.CreateClient(name). Typed Clients offer better separation of concerns and facilitate unit testing by encapsulating configuration and business logic. Named Clients are useful for simple cases where multiple different configurations are needed without complex business logic.

3

How to configure a global timeout for all HttpClient instances in an application?

Câu trả lời

Configuring the timeout via ConfigurePrimaryHttpMessageHandler allows defining a timeout at the handler level that will apply to all clients created by the factory. This approach centralizes configuration and avoids code repetition. It's also possible to configure the timeout individually on each HttpClient, but this becomes difficult to maintain in a large application with multiple configured clients.

4

Which method allows reading the body of an HTTP response as a string?

5

What is the role of a DelegatingHandler in the HttpClient pipeline?

+17 câu hỏi phỏng vấn

Nắm vững .NET cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí