Concurrent IO read or write operations with HTTPWebRequest

Recently when using the HTTPWebRequest class to stream a file up to a SharePoint Online Document Library, I was consistently coming across the following issue:

System.NotSupportedException: The stream does not support concurrent IO read or write operations.

What was really annoying about that message is, I was not doing any concurrent operations but passing blocks of data one at a time. I also noticed it was consistently bombing out around the 2MB stage of the upload.

This was resolved with setting the AllowWriteStreamBuffering property to false.

Happy days! The file was uploading beautifully at whatever size I wanted; until I started occasionally getting the below error when calling GetResponse() on the same stream after I had uploaded the file:

This request requires buffering data to succeed

This was really annoying; but I quickly realised one could change the AllowWriteStreamBuffering back to true after the upload and before calling GetResponse()

After I did that, all was well in the world; and we all lived happily ever after.

Note that this only was affecting me in SharePoint Online; OnPrem servers need AllowWriteStreamBuffering to be true, otherwise the connection will be closed before it completes.