.NET

HttpClient & Networking

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

20 pytań z rozmów·
Mid-Level
1

Why use IHttpClientFactory instead of instantiating HttpClient directly?

Odpowiedź

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?

Odpowiedź

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?

Odpowiedź

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 pytań z rozmów

Opanuj .NET na następną rozmowę

Uzyskaj dostęp do wszystkich pytań, flashcards, testów technicznych, ćwiczeń code review i symulatorów rozmów.

Zacznij za darmo