bloomtom.HttpProgress 2.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package bloomtom.HttpProgress --version 2.2.1                
NuGet\Install-Package bloomtom.HttpProgress -Version 2.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="bloomtom.HttpProgress" Version="2.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add bloomtom.HttpProgress --version 2.2.1                
#r "nuget: bloomtom.HttpProgress, 2.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install bloomtom.HttpProgress as a Cake Addin
#addin nuget:?package=bloomtom.HttpProgress&version=2.2.1

// Install bloomtom.HttpProgress as a Cake Tool
#tool nuget:?package=bloomtom.HttpProgress&version=2.2.1                

HttpProgress

A set of extension methods for HttpClient which adds progress reporting.

HttpClient doesn't natively support progress reporting? Yes, it's true. You're expected to make your own HttpContent which overrides SerializeToStreamAsync for PutAsync and PostAsync, and you're expected to extend GetAsync with progress reporting in the stream copy logic. Well guess what? I've done that for you.

Nuget Packages

Package Name Target Framework Version
HttpProgress .NET Standard 2.0 NuGet

Usage

Add HttpProgress to your usings.

using HttpProgress;

Use the HttpClient extensions.

// Make your progress event.
var progress = new Progress<ICopyProgress>(x => // Please see "Notes on IProgress<T>"
{
    // This is your progress event!
    // It will fire on every buffer fill so don't do anything expensive.
    // Writing to the console IS expensive, so don't do the following in practice...
    Console.WriteLine(x.PercentComplete.ToString("P"));
});

// Post
using (var fileStream = System.IO.File.OpenRead("MyFile.txt"))
{
    var result = await client.PostAsync("https://mysite.com/something", fileStream, progress);
}

// Get
using (var downloadStream = new MemoryStream())
{
    var response = await client.GetAsync("https://mysite.com/something", downloadStream, progress);
}

Woah, that was easy. But what if you want more than just percent complete? Well the progress event actually gives you all of the following in ICopyProgress:

public interface ICopyProgress
{
	/// <summary>
	/// The instantaneous data transfer rate.
	/// </summary>
	int BytesPerSecond { get; }
	/// <summary>
	/// The total number of bytes transfered so far.
	/// </summary>
	long BytesTransfered { get; }
	/// <summary>
	/// The total number of bytes expected to be copied.
	/// </summary>
	long ExpectedBytes { get; }
	/// <summary>
	/// The percentage complete as a value 0-1.
	/// </summary>
	double PercentComplete { get; }
	/// <summary>
	/// The total time elapsed so far.
	/// </summary>
	TimeSpan TransferTime { get; }
}

Notes on IProgress<T>

Concrete Implementation

The type Progress<T> is provided by the framework for use cases where the progress changed event should be processed through your application's synchronization context. This is useful for desktop UI applications where modifying controls from the progress event needs to be done on the event loop thread. For console applications, which have no intelligent synchronization context, the event will end up on the thread pool and will likely be executed out of order. If this is undesirable, you can use your own implementation of IProgress. Doing this is very easy, and an example is provided in the unit test project class NaiveProgress<T>.

Performance Considerations

The action you give for progress reporting will fire on every buffer cycle. This can happen many times! With a 16kB buffer transfering a 10MB file will cause 640 events to be fired.

If you need to do time consuming operations, consider rate limiting them. A simple way to do this is to only fire your expensive operation when TransferTime or PercentComplete crosses specific thresholds. A more complex but "prettier" solution is to buffer ICopyProgress and have a threaded reader to refresh your UI on a timer.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on bloomtom.HttpProgress:

Package Downloads
bloomtom.BunAPI

A simple API for file management on BunnyCDN's file storage. https://bunnycdn.com

bloomtom.MStorage

Bridges multiple storage backends into a simple object store layer.

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on bloomtom.HttpProgress:

Repository Stars
t1m0thyj/WinDynamicDesktop
Port of macOS Mojave Dynamic Desktop feature to Windows
CircumSpector/DS4Windows
A reimagination of DS4Windows.
Mongo2Go/Mongo2Go
Mongo2Go - MongoDB for .NET integration tests
Version Downloads Last updated
2.3.2 18,274 1/29/2020
2.3.1 24,548 1/26/2019
2.3.0 1,619 12/31/2018
2.2.2 1,190 12/30/2018
2.2.1 1,101 12/30/2018
2.2.0 1,108 12/30/2018
2.1.0 1,496 12/28/2018
2.0.2 1,897 12/20/2018
2.0.1 1,200 12/20/2018
2.0.0 1,303 12/19/2018
1.0.5 1,553 11/25/2018
1.0.4 1,312 11/25/2018
1.0.3 1,302 11/25/2018
1.0.2 1,326 11/25/2018
1.0.1 1,362 11/25/2018
1.0.0 1,122 11/25/2018