WasiInMemoryHttpClient Class

An HTTP client that gives the requests to a handler of the process, without the network: a HttpMessageHandler (such as a mock), or a function. It works on every platform, including the browser.

public sealed class WasiInMemoryHttpClient : WasiHttpClient

Example

// The requests of the component go to a function of the process, without the network
var client = new WasiInMemoryHttpClient((request, cancellationToken) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new StringContent("Hello from " + request.RequestUri!.Host),
}));

var wasi = new WasiComponentContext(new WasiOptions { HttpClient = client });
Inheritance
WasiHttpClient WasiInMemoryHttpClient

Constructors

NameDescription
WasiInMemoryHttpClient(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>>)Initializes a new instance of the WasiInMemoryHttpClient class with a function.
WasiInMemoryHttpClient(HttpMessageHandler)Initializes a new instance of the WasiInMemoryHttpClient class with a message handler.

WasiInMemoryHttpClient overloads

WasiInMemoryHttpClient(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>>)

WasiHttpClient.cs

Initializes a new instance of the WasiInMemoryHttpClient class with a function.

public WasiInMemoryHttpClient(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler)
Parameters
handler Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>>
The function that returns the response of a request.

WasiInMemoryHttpClient(HttpMessageHandler)

WasiHttpClient.cs

Initializes a new instance of the WasiInMemoryHttpClient class with a message handler.

public WasiInMemoryHttpClient(HttpMessageHandler handler)
Parameters
handler HttpMessageHandler
The handler of the requests: the client does not dispose it.

Methods

NameDescription
SendAsync(HttpRequestMessage, CancellationToken)Sends a request, and returns the response once its headers are received.

SendAsync(HttpRequestMessage, CancellationToken)

WasiHttpClient.cs

Sends a request, and returns the response once its headers are received.

public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

Parameters

request HttpRequestMessage
The request.
cancellationToken CancellationToken
The token that is cancelled when the component cancels the request.

Returns

Task<HttpResponseMessage> The response.