HttpRecordingHandler Class

A delegating handler that records and replays HTTP interactions for testing.

public sealed class HttpRecordingHandler : DelegatingHandler, IAsyncDisposable
Inheritance
DelegatingHandler HttpRecordingHandler
Implements

Constructors

NameDescription
HttpRecordingHandler(IHttpRecordingStore, HttpRecordingOptions?)Initializes a new instance of the HttpRecordingHandler class without an inner handler.
HttpRecordingHandler(HttpMessageHandler, IHttpRecordingStore, HttpRecordingOptions?)Initializes a new instance of the HttpRecordingHandler class with an inner handler.

HttpRecordingHandler overloads

HttpRecordingHandler(IHttpRecordingStore, HttpRecordingOptions?)

HttpRecordingHandler.cs

Initializes a new instance of the HttpRecordingHandler class without an inner handler.

public HttpRecordingHandler(IHttpRecordingStore store, HttpRecordingOptions? options = null)
Parameters
store IHttpRecordingStore
The store used to load and save recorded entries.
options HttpRecordingOptions?
The recording options. Because there is no inner handler, no real HTTP call can be made, so the mode must be HttpRecordingMode.Replay and the miss behavior must not be HttpRecordingMissBehavior.Passthrough.

HttpRecordingHandler(HttpMessageHandler, IHttpRecordingStore, HttpRecordingOptions?)

HttpRecordingHandler.cs

Initializes a new instance of the HttpRecordingHandler class with an inner handler.

public HttpRecordingHandler(HttpMessageHandler innerHandler, IHttpRecordingStore store, HttpRecordingOptions? options = null)
Parameters
innerHandler HttpMessageHandler
The inner handler.
store IHttpRecordingStore
The store used to load and save recorded entries.
options HttpRecordingOptions?
The recording options.

Methods

NameDescription
DisposeAsync()Saves the recordings when HttpRecordingOptions.AutoSave is enabled, then disposes the handler.
InitializeAsync(CancellationToken)Loads existing recordings from the store. If not called explicitly, the first HttpRecordingHandler.SendAsync call will call it automatically.
SaveAsync(CancellationToken)Saves all recorded entries to the store, waiting for any in-flight recording to complete first.

Saves the recordings when HttpRecordingOptions.AutoSave is enabled, then disposes the handler.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask

Remarks

The save is not skipped when the handler has already been disposed synchronously: an HttpClient owns its handler by default and disposes it first, so by the time this runs the handler is usually disposed already. The recorded entries are still in memory and are what needs persisting.

InitializeAsync(CancellationToken)

HttpRecordingHandler.cs

Loads existing recordings from the store. If not called explicitly, the first HttpRecordingHandler.SendAsync call will call it automatically.

net10.0

public Task InitializeAsync(CancellationToken cancellationToken = default)

net11.0

public Task InitializeAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

Remarks

Nothing is loaded in HttpRecordingMode.Record mode: saving there replaces the store's content instead of appending to it.

SaveAsync(CancellationToken)

HttpRecordingHandler.cs

Saves all recorded entries to the store, waiting for any in-flight recording to complete first.

public Task SaveAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task