gpt4 book ai didi

c# - 文件上传到 Web API 和 Azure 抛出 HttpException

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:07 25 4
gpt4 key购买 nike

有时,当中国用户将小文件 (100-200kB) 上传到将文件直接传递到 Azure blob 的 WebAPI 服务时,我会遇到此异常:

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

Controller 代码如下所示:

[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);
}

以及堆栈跟踪:

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()

可以安全地假设这只是互联网连接不良吗?在这种情况下,我可以捕捉并忽略它。客户端有一个安全的重试机制,以防上传出错。

或者我的代码有问题吗?

最佳答案

代码对我来说看起来不错,在这种情况下重试是有意义的。

您可能还想为您的存储帐户启用存储分析,这样如果下次您遇到此异常,您将能够通过检查日志来判断请求是否已到达 Azure 存储服务器端,请参阅有关详细信息,请参阅下面帖子中的“端到端跟踪”部分。

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

关于c# - 文件上传到 Web API 和 Azure 抛出 HttpException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28085780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com