C# 将FromStream上载到Azure Blob时超出了最大请求长度异常
我编写了一个API,试图通过c#中的流将HTTP post内容(我试图上传视频)上传到Azure blobs。 像这样的事情:C# 将FromStream上载到Azure Blob时超出了最大请求长度异常,c#,asp.net,web-services,http,azure,C#,Asp.net,Web Services,Http,Azure,我编写了一个API,试图通过c#中的流将HTTP post内容(我试图上传视频)上传到Azure blobs。 像这样的事情: Stream videoStream = await Request.Content.ReadAsStreamAsync(); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); blockBlob.UploadFromStream(videoStream); System.Web
Stream videoStream = await Request.Content.ReadAsStreamAsync();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
blockBlob.UploadFromStream(videoStream);
System.Web.HttpException (0x80004005): Maximum request length exceeded.
at System.Web.HttpBufferlessInputStream.ValidateRequestEntityLength()
at System.Web.HttpBufferlessInputStream.GetPreloadedContent(Byte[] buffer, Int32& offset, Int32& count)
at System.Web.HttpBufferlessInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at Microsoft.Owin.Host.SystemWeb.CallStreams.DelegatingStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Web.Http.NonOwnedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Http.DelegatingStream.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 Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
它在我当地的环境中工作正常。但当我将此web服务部署到Azure并调用API时,我得到了一个“超过最大请求长度”的异常,如下所示:
Stream videoStream = await Request.Content.ReadAsStreamAsync();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
blockBlob.UploadFromStream(videoStream);
System.Web.HttpException (0x80004005): Maximum request length exceeded.
at System.Web.HttpBufferlessInputStream.ValidateRequestEntityLength()
at System.Web.HttpBufferlessInputStream.GetPreloadedContent(Byte[] buffer, Int32& offset, Int32& count)
at System.Web.HttpBufferlessInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at Microsoft.Owin.Host.SystemWeb.CallStreams.DelegatingStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Web.Http.NonOwnedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Http.DelegatingStream.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 Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
我已经根据Web上的一些教程更改了我的Web.config
<system.web>
<httpRuntime targetFramework="4.5" maxRequestLength="600000" executionTimeout="3600" />
<compilation debug="true" targetFramework="4.5" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="600000000" />
</requestFiltering>
</security>
</system.webServer>
但是在函数copytosync中抛出了相同的异常“超过了最大请求长度”
System.Web.HttpException (0x80004005): Maximum request length exceeded.
at System.Web.HttpBufferlessInputStream.ValidateRequestEntityLength()
at System.Web.HttpBufferlessInputStream.GetPreloadedContent(Byte[] buffer, Int32& offset, Int32& count)
at System.Web.HttpBufferlessInputStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at Microsoft.Owin.Host.SystemWeb.CallStreams.DelegatingStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.Web.Http.NonOwnedStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.Net.Http.StreamToStreamCopy.StartRead()
下面是一个示例,它将一个随机20MB字节数组从控制台客户端上载到Web API控制器,该控制器将HTTP请求内容流式传输到Azure blob存储: 请注意,它在客户端缓冲(在内存中构建整个20MB字节数组),但不在服务器上缓冲 我在我的笔记本电脑上运行了客户端,服务器在本地和Azure Web应用程序中运行(没有额外的配置,只是将Web API项目发布到Azure Web应用程序中),成功地测试了这一点
希望这有助于。。。祝你好运 对Web.Config文件进行更改 上面的解决方案是可行的,我想我只需要添加必要的代码更改,而不需要下载解决方案并在其中搜索以找到需要的更改。在system.web选项卡之间添加以下内容,这些选项卡之间应该已经存在其他信息
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="600000" executionTimeout="3600" />
</system.web>
<system.webserver>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="600000000" />
</requestFiltering>
</security>
我希望这能帮助别人。谢谢JoshL,看来你的代码和我的代码完全一样。但我的仍然无法在Azure Web应用程序上运行。我的本地IIS版本是10.0,但Azure似乎是8.0,我怀疑可能存在问题。还在查…啊,听到这个消息很抱歉。。。如果您有更多信息要共享,请告诉我们,我们可以帮助您仔细查看。出于好奇,您确定您的网站运行在Azure的.NET 4.5而不是其他版本上吗?您的web.config特别引用了targetFramework=4.5。您可以根据此处查看和修改Azure站点配置。。。我检查了我的本地环境和azure设置,这两个平台都使用.NET4.6。我试图删除web.config中的targetFramework=4.5,并试图将其修改为targetFramework=4.6,但似乎没有生效。本地成功,Azure失败。请尝试完全删除targetFramework引用。。。这会改变什么吗?