Xamarin AWS S3 iOS下载“错误:ConnectFailure(打开的文件太多)”

时间:2018-07-20 15:59:39

标签: amazon-web-services amazon-s3 xamarin.ios aws-sdk-net

我正在iOS项目的Xamarin上使用适用于.NET的AWS开发工具包,并且遇到了麻烦。从记录来看,我对Xamarin和一般的移动开发还比较陌生。

在我的实际应用程序中,我正在从S3下载文件并将其存储在本地数据库中。在示例中,我只是在破坏数据。

我正在使用多达10个线程下载320个文件,并且在大约250个文件之后,出现错误“错误:ConnectFailure(打开的文件太多)”。我知道iOS的最大打开文件描述符限制为250,但按照书面规定,每个线程只能打开1个(10)。

我花了将近3天的时间,尝试直接调用dispose方法,直接调用GC,处置和重新创建S3Client以及许多其他事情,但是没有任何改变。

我花了很多时间,因为这似乎是S3的一个非常常见的应用程序,我发现很难相信SDK中存在如此重要的错误,而没有看到很多人在讨论它。

代码:

    public async static Task Test(CognitoAWSCredentials credentials)
    {
        SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(10);
        List<Task> taskList = new List<Task>();

        AmazonS3Config config = new AmazonS3Config();
        config.RegionEndpoint = Amazon.RegionEndpoint.USEast1;

        AmazonS3Client client = new AmazonS3Client(credentials, config);

        foreach (string key in _fileNames) //_fileNames is an array of 320 S3 keys
        {
            await concurrencySemaphore.WaitAsync();

            Task task = Task.Run(
                async () =>
                {
                    byte[] trash;
                    try
                    {
                        Trace.Message("S3: Downloading key '" + key + "'");

                        GetObjectRequest request = new GetObjectRequest()
                        {
                            BucketName = "LucentWearDesigns",
                            Key = key
                        };

                        using (GetObjectResponse response = await client.GetObjectAsync(request))
                        {
                            using (BinaryReader reader = new BinaryReader(response.ResponseStream))
                            {
                                // Read data
                                int length = (int)response.ContentLength;

                                trash = reader.ReadBytes(length);
                            }
                        }
                    }
                    catch (Exception x)
                    {
                        Trace.Message("S3: error downloading key: " + key);
                        Trace.Message(x.Message);
                        Trace.Message(x.StackTrace);
                        throw;
                    }
                    finally
                    {
                        concurrencySemaphore.Release();
                    }

                });

            taskList.Add(task);
        }

        //Wait until all are finished
        await Task.WhenAll(taskList);
        Trace.Message("Synch: Success");
    }

错误和堆栈跟踪:

“错误:ConnectFailure(打开的文件太多)”

/Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/System中的System.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult asyncResult)[0x00059]上的

' /System.Net/HttpWebRequest.cs:1033   在System.Threading.Tasks.TaskFactory 1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func 2 [T,TResult] endFunction,System.Action 1[T] endAction, System.Threading.Tasks.Task 1 [TResult] Promise,System.Boolean requireSynchronization)中:[0x0000f],在:0中 ---从之前引发异常的位置开始的堆栈结束跟踪---   在Amazon.Runtime.HttpWebRequestMessage + d__20.MoveNext()[0x0015d]中:: 0 ---从之前引发异常的位置开始的堆栈结束跟踪---   在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] /exceptionservices/exceptionservicescommon.cs:152   在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x00037] /referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187   在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class中的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[0x00028] /referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156   在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class中的System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task任务)[0x00008] /referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128   在System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <c8e3aece7e434bb994fb57ae63a2f189>:0 at Amazon.Runtime.Internal.HttpHandler 1 + d__9 1[TRequestContent,T].MoveNext () [0x0027d] in <e3b0afe3769d4ae68440b96fd7ea4c11>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1 + ConfiguredTaskAwaiter [TResult] .GetResult()[0x00000]中位于:0   在Amazon.Runtime.Internal.RedirectHandler + d__1 1[T].MoveNext () [0x00078] in <e3b0afe3769d4ae68440b96fd7ea4c11>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1 + ConfiguredTaskAwaiter [TResult] .GetResult()[0x00000]中:: 0   在Amazon.Runtime.Internal.Unmarshaller + d__3 1[T].MoveNext () [0x00079] in <e3b0afe3769d4ae68440b96fd7ea4c11>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.177/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1 + ConfiguredTaskAwaiter [TResult] .GetResult()[0x00000]中的位置:: 0   在<5f4780c37ef344fbb8faf1b8ae9ebc43>:0'

中的Amazon.S3.Internal.AmazonS3ResponseHandler + d__1`1 [T] .MoveNext()[0x0006f]中

在我接近该项目的最后期限之前,任何帮助将不胜感激。谢谢!

0 个答案:

没有答案