Aws-sdk-net: 不允许进行同步操作。 调用ReadAsync或将AllowSynchronousIO设置为true。

创建于 2020-02-21  ·  4评论  ·  资料来源: aws/aws-sdk-net

AWS开发工具包正在异步调用上进行同步读取

全栈跟踪:

System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.Stream.Read(Span`1 buffer)
   at Amazon.Runtime.Internal.Util.WrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at Amazon.Runtime.Internal.Util.PartialReadOnlyWrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at Amazon.Runtime.Internal.Util.WrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at Amazon.Runtime.Internal.Util.HashStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at Amazon.Runtime.Internal.Util.ChunkedUploadWrapperStream.FillInputBuffer()
   at Amazon.Runtime.Internal.Util.ChunkedUploadWrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at Amazon.Runtime.Internal.Util.WrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.Stream.<>c.<BeginReadInternal>b__43_0(Object <p0>)
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
   at System.IO.Stream.CopyToAsyncInternal(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
   at System.Net.Http.StreamToStreamCopy.<CopyAsync>g__DisposeSourceAsync|0_0(Task copyTask, Stream source)
   at System.Net.Http.HttpContent.CopyToAsyncCore(ValueTask copyTask)
   at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.SendRequestContentWithExpect100ContinueAsync(HttpRequestMessage request, Task`1 allowExpect100ToContinueTask, HttpContentWriteStream stream, Timer expect100Timer, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
   at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.RedirectHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.S3.Internal.AmazonS3ResponseHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.S3.Internal.AmazonS3ExceptionHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)

复制(首先配置_client ):

public class Foo {
    private class File
    {
       public long? Filesize { get;set; }   
    }

    AmazonS3Client _client;
   string _bucket;

    public async Task Upload(File file, Stream stream)
    {
        long effectiveLength = 0;
        if (file.Filesize != null) 
        {
            if (!stream.CanSeek)
                throw new ArgumentException("Unseekable streams have to pass in a file size"); 
            effectiveLength = stream.Length - stream.Position;
        }
        else 
        {
            effectiveLength = file.Filesize.Value;
        }

        var request = 
            new PutObjectRequest(
                AutoResetStreamPosition = stream.CanSeek,
                Key = "bar",
                BucketName = _bucket,
                InputStream = stream
            )
        request.Headers.ContentLength = effectiveLength;

        await _client.PutObjectAsync(request)
    }
}

Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream调用Upload ,例如,通过http上传文件到控制器操作,该操作将调用Upload ,AspNetCore 3.0及更高版本中的任何方法都可以。

之所以发生这种情况,是因为Amazon.Runtime.Internal.Util中的所有流实际上都不会覆盖任何Async方法(ReadAsync,BeginRead); 这些会默认调用同步版本。

有关:
https://github.com/aws/aws-sdk-net/issues/675
[公告]所有服务器均禁用AllowSynchronousIO

A bug modulsdk-core queued

最有用的评论

@NinoFloris我们知道这个问题。 这对于完成WASM Blazor单线程支持也至关重要。 我们正在PR上进行更新,以更新所有流实现以具有异步实现。 我希望能尽快为您提供发布状态的更新。

所有4条评论

@normj如果您对自己的生物中的

@NinoFloris我们知道这个问题。 这对于完成WASM Blazor单线程支持也至关重要。 我们正在PR上进行更新,以更新所有流实现以具有异步实现。 我希望能尽快为您提供发布状态的更新。

这里有更新吗? 看起来#1416正在添加ReadAsync ,这是必需的。

跟踪情况如何? 自.NET 4.5(8年前发布)以来, Stream.ReadAsync已经存在,并且异步已成为当今的主流。 实际上,如果您只给我异步的话,我会很乐意放弃同步版本。

如果您可以提升此库使其与现代dotnet兼容,我将不胜感激。

此页面是否有帮助?
0 / 5 - 0 等级