Benjamin E.
Benjamin E.

Reputation: 5162

File upload to WebAPI & Azure throws HttpException

Sometimes I get this exception when users from China upload small files (100-200kB) to a WebAPI service that passes the file straight into an Azure blob:

System.Web.HttpException: The client is disconnected because the underlying request has been completed. There is no longer an HttpContext available.

The controller code looks like this:

[HttpPost]
public async Task<IHttpActionResult> Upload()
{
    var stream = await Request.Content.ReadAsStreamAsync();

    var guid = Guid.NewGuid().ToString();
    var blob = /*CloudBlobContainer*/.GetBlockBlobReference(guid);

    blob.Properties.ContentType = Request.Content.Headers.ContentType.MediaType;
    blob.UploadFromStream(stream);

    return Ok(guid);
}

And the stacktrace:

System.Web.HttpException: The client is disconnected because the underlying request has been completed.  There is no longer an HttpContext available.

at System.Web.HttpBufferlessInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)

at System.Web.Http.WebHost.SeekableBufferedRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count)

at Microsoft.WindowsAzure.Storage.Core.Util.StreamExtensions.WriteToSync[T](Stream stream, Stream toStream, Nullable`1 copyLength, Nullable`1 maxLength, Boolean calculateMd5, Boolean syncRead, ExecutionState`1 executionState, StreamDescriptor streamCopyState)

at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamHelper(Stream source, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)

at .Controllers.FileController.<Upload>d__5.MoveNext()

Is it safe to assume this is just the bad internet connection? In that case I could catch and ignore it. The client has a safe re-try mechanism in case an upload went wrong.

Or is something wrong with my code?

Upvotes: 2

Views: 1308

Answers (1)

Jason Tang - MSFT
Jason Tang - MSFT

Reputation: 315

The code looks OK to me, and it makes sense to have the retry in that case.

You may also want turn on Storage Analytics for your storage account so if next time you hit this exception, you will be able to tell if the request has reached Azure Storage Server side or not by checking the log, please refer to the "End-to-end tracing" part in the post below for details.

http://azure.microsoft.com/en-us/documentation/articles/storage-monitoring-diagnosing-troubleshooting/

Upvotes: 1

Related Questions