When using HttpClient.SendAsync() to access a resource that might take a while to process it can be advantageous to detect when the connection has been disconnected.
This can be done using a CancellationToken passed to the HttpClient.SendAsync()
method.
public class HttpRequestHandler : IHttpRequestHandler
{
public CancellationToken CancellationToken => _cancel;
public HttpRequestHandler(IHttpContextAccessor httpContext)
{
_cancel = httpContext?.HttpContext?.RequestAborted ?? CancellationToken.None;
}
/// <summary>
/// Execute request and return response.
/// </summary>
public virtual async Task<IResponse<T>> ExecuteAsync<T>(IRequest request) where T : class, new()
{
// ...
var response = await this.GetClient(request).SendAsync(request1, completionOption, CancellationToken).ConfigureAwait(false);
// ...
}
}
You must be logged in to see the comments. Log in now!