Miz Miz - 1 month ago 5x
C# Question

C# WebClient upload speeds

I was wondering if it is possible to increase buffer size on WebClient Async data upload, because currently it pushes ~320kB/s maximum.

My current code:

using (WebClient Client = new WebClient())
byte[] Buffer = File.ReadAllBytes(this.WorkItem.FileLocation);

Client.UploadProgressChanged += new UploadProgressChangedEventHandler(Client_UploadProgressChanged);
Client.UploadDataCompleted += new UploadDataCompletedEventHandler(Client_UploadDataCompleted);
Client.UploadDataAsync(new Uri("-snip-"), Buffer);


Connection is not the limiting factor. ( its 300mbit connection, web-servers push content at ~30-40mB/s mark )


If you want more control over how the data is buffered you need to use the HttpWebRequest class. With this class you can choose your read buffer reading from a FileStream and then how much you are writing to the network stream. Doing 4MB reads and 32KB writes was optimal for maxing out my network throughput (although you will have to do your own benchmarks to see which buffers work best in your scenario).