.NET

HttpClient & Networking

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

20 питань зі співбесід·
Mid-Level
1

Why use IHttpClientFactory instead of instantiating HttpClient directly?

Відповідь

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?

Відповідь

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?

Відповідь

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 питань зі співбесід

Опануй .NET для наступної співбесіди

Отримай доступ до всіх питань, flashcards, технічних тестів, вправ code review та симуляторів співбесід.

Почни безкоштовно