public async Task <HttpResponseMessage> InvokeAsync(ServerTestCommandInfo commandInfo, TaskExecutionContext context, CancellationToken cancellationToken) { for (int i = 0; i < 100; i++) { await Task.Delay(400, cancellationToken); context.ReportProgress(i / 100d); context.ReportStatus($"Doing some stuff ({i}%)"); } return(new HttpResponseMessage(HttpStatusCode.OK)); }
private static async Task <bool> WriteData(Stream targetStream, FileSource fileSource, TaskExecutionContext context, ILogger logger) { if (fileSource.Data.Scheme == FileSource.Base64Scheme) { logger?.LogDebug("Write Base64 binary to file"); context.ReportStatus("Write Base64 to file..."); var buffer = Convert.FromBase64String(fileSource.Data.AbsolutePath); targetStream.Write(buffer, 0, buffer.Length); return(true); } if (fileSource.Data.Scheme == "http" || fileSource.Data.Scheme == "https") { context.ReportStatus("Download headers..."); var httpClient = context.Services.GetRequiredService <IHttpClientService>().Client; using (var response = await httpClient.GetAsync(fileSource.Data, HttpCompletionOption.ResponseHeadersRead)) { if (!response.IsSuccessStatusCode) { logger?.LogError("Requesting {uri} failed with status {status}.", fileSource.Data, response.StatusCode); return(false); } using (var contentStream = await response.Content.ReadAsStreamAsync()) { var buffer = ArrayPool <byte> .Shared.Rent(8192); try { var contentLength = response.Content.Headers.ContentLength; var totalDataLoaded = 0; while (true) { var read = await contentStream.ReadAsync(buffer, 0, buffer.Length); if (read == 0) { break; } targetStream.Write(buffer, 0, read); totalDataLoaded += read; if (contentLength != null) { context.ReportProgress((double)totalDataLoaded / contentLength); } context.ReportStatus($"Downloading... ({DataSizeFormatter.BytesToString(totalDataLoaded)})"); } } finally { ArrayPool <byte> .Shared.Return(buffer); } } return(true); } } throw new ArgumentOutOfRangeException(nameof(fileSource), fileSource.Data.Scheme, "The scheme is not supported as file source."); }