Monday, October 05, 2009

Transferring large files > 512 MB using C# HttpWebRequest

When transferring large files over the network using C#, there is a good chance that your application will be thwarted by either:
- System.IO.Exception
- System.Net.WebException

Depending on the order in which you catch exceptions, you might spend a good deal of time debugging the root cause of the problem. In my case, I was trying to send a 4GB file over the wire; every time I tried, the upload would fail with the message:

System.Net.WebException: The request was aborted: The request was canceled.
System.IO.IOException: Cannot clos e stream until all bytes are written.

After a lot of debugging and speculation, the actual cause of the exception was not related to my use of Read/Write with the Stream(s) I chose for the operations. The issue was with the way the HttpWebRequest was configured, in particular, the timeout values associated with the request.

Refer the Properties section of HttpWebRequest@MSDN and look at the description for ReadWriteTimeout. The default value for this timeout, 300 seconds, was too low for the amount of data being transferred, resulting in the write being aborted prematurely. The only consequence of such an action is the exception I stated earlier. The fix: simple - increase the timeout to a reasonable value; our application has this set to 10 minutes.

No comments:

Post a Comment