ProxyMessageHandler
Namespace: RA.Utilities.Integrations.DelegatingHandlers
First, a key clarification: ProxyMessageHandler is a static factory class, not a
DelegatingHandler.
This is not a
DelegatingHandler
because proxy settings must be applied to the primary message handler.
This distinction is crucial.
The primary purpose of ProxyMessageHandler is to centralize the logic for creating and configuring the innermost
DelegatingHandler
with proxy settings.
Here’s why that's necessary:
-
HttpClientPipeline Architecture: AnHttpClient's request pipeline consists of a chain of handlers. The outermost handlers areDelegatingHandler(like your logging and auth handlers), which can inspect or modify a request and then pass it down the chain. The very last handler in the chain is the primary message handler (usually anHttpClientHandler), which is responsible for actually sending the request over the network. -
Proxy Configuration Requirement: Proxy settings cannot be applied by a
DelegatingHandler. They must be configured directly on the properties of the primaryHttpClientHandlerbefore the pipeline is even built. -
A Centralized Factory: The
ProxyMessageHandleracts as a factory to solve this problem. Its static Create method takes anIProxySettingsobject and returns a correctly configuredHttpClientHandler.
- If no proxy settings are provided, it returns a default
HttpClientHandler. - If proxy settings are provided, it creates a
WebProxyinstance, populates it with the address, credentials, and bypass settings, and assigns it to a newHttpClientHandler.