5.33Setting the Request Event Handler
The IHttpRequestEventHandler interface lets you define preprocessing operations for HTTP requests. By implementing this interface, you can create a custom request event handler, such as the CustomRequestEventHandler class:
public class CustomRequestEventHandler : IHttpRequestEventHandler{ // Operation before sending the HTTP request public void OnPreSendRequest(HttpRequestMessage httpRequestMessage) {} // Operation after receiving the HTTP response public Task OnPostReceiveResponseAsync(HttpResponseMessage httpResponseMessage, CancellationToken cancellationToken) {} // Operation when an exception occurs while sending the HTTP request public void OnRequestFailed(Exception exception, HttpResponseMessage? httpResponseMessage = null) {}}When sending an HTTP request, you can set the request event handler by adding the RequestEventHandlerAttribute attribute.
HTTP declarative requests set the request event handler through the RequestEventHandlerAttribute attribute. The corresponding HTTP declarative extractor implementation is the RequestEventHandlerDeclarativeExtractor type, which is responsible for parsing the RequestEventHandlerAttribute attribute and building the request event handler configuration required by the HttpRequestBuilder instance.
// Applied at the interface definition, affecting all methods[RequestEventHandler(typeof(CustomRequestEventHandler))]public interface IHttpService : IHttpDeclarative{ // Automatically applies the interface declaration [Get("https://furion.net/")] Task<string> GetStringAsync(); // Applied at the method level [RequestEventHandler(typeof(CustomRequestEventHandler))] [Get("https://furion.net/")] Task<string> GetStringAsync();}RequestEventHandlerAttribute contains the following constructors and properties:
-
Constructors:
new(handlerType): Applies to methods or interfaces, configuring the request event handler.
-
Properties:
HandlerType: The request event handler (of typeType).