The Cause: Why You Cannot See Handlers Using Normal Testing
The main reason regular tests cannot inspect HttpClient handlers is simple: the pipeline is private. The HttpClient instance created by IHttpClientFactory stores its entire message-handler chain inside a non-public field named _handler on its base class HttpMessageInvoker.
This means:
- there is no public property to read the handler list
- DI registration only confirms setup, not actual construction
- mocks cannot expose the real pipeline
- even typed clients hide the underlying handler chain
So while Visual Studio’s debugger can show the handler sequence, your code cannot. This is why common testing approaches fail: they operate at the service level, not the internal pipeline level.
A service class typically stores a protected or private HttpClient instance:
protected readonly HttpClient _httpClient;
Even if your test resolves this service, the handler pipeline remains invisible.
To validate the runtime configuration—exactly as it will behave in production—you must inspect the pipeline directly. Since .NET does not expose it, the only practical method is to use reflection. The next Snipp explains how to implement this in a clean and repeatable way.
Comments