DynamoDb SocketException:尝试对无法访问的网络进行套接字操作

时间:2018-11-06 17:00:34

标签: windows .net-core amazon-dynamodb

我正在尝试连接到使用docker在本地运行的DynamoDb: docker run -p 8000:8000 dwmkerr/dynamodb -sharedDb

但是我一直收到标题中提到的错误。

我可以验证DynamoDb服务器正在运行并且可以访问。

我可以访问网址http://localhost:8000/shell/

我什至尝试使用nodejs客户端https://github.com/aaronshaf/dynamodb-admin并设法创建了一些表。

但是,只有dotnet代码无法运行!

class Program
{
    static void Main(string[] args)
    {
        var clientConfig = new AmazonDynamoDBConfig()
        {
            UseHttp = true,
            LogMetrics = true,
            LogResponse = true,
            DisableLogging = false,
            ServiceURL = "http://localhost:8000"
        };

        var dynamoClient = new AmazonDynamoDBClient(clientConfig);

        //AWSSDK
        var request = new CreateTableRequest
        {
            TableName = "Todo",
            KeySchema = new List<KeySchemaElement> { new KeySchemaElement("Id", KeyType.HASH), },
            AttributeDefinitions = new List<AttributeDefinition> { new AttributeDefinition("Id", ScalarAttributeType.N), },
            ProvisionedThroughput = new ProvisionedThroughput
            {
                ReadCapacityUnits = 10,
                WriteCapacityUnits = 5,
            }
        };
        dynamoClient.CreateTableAsync(request).Wait();
    }
}

这是完整的堆栈跟踪:

Unhandled Exception: System.AggregateException: One or more errors occurred. (A socket operation was attempted to an unreachable network) ---> System.Net.Http.HttpRequestException: A socket operation was attempted to an unreachable network ---> System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable network
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
   at Amazon.Runtime.Internal.Util.AsyncHelpers.<>c__DisplayClass1_1`1.<<RunSync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Amazon.Runtime.Internal.Util.AsyncHelpers.ExclusiveSynchronizationContext.BeginMessageLoop() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\_mobile\AsyncHelpers.cs:line 142
   at Amazon.Runtime.Internal.Util.AsyncHelpers.RunSync[T](Func`1 task) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\_mobile\AsyncHelpers.cs:line 87
   at Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri, TimeSpan timeout, IWebProxy proxy) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\AWSSDKUtils.cs:line 969
   at Amazon.Util.EC2InstanceMetadata.GetItems(String relativeOrAbsolutePath, Int32 tries, Boolean slurp) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\_bcl+coreclr\EC2InstanceMetadata.cs:line 513
   at Amazon.Util.EC2InstanceMetadata.get_IAMSecurityCredentials() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\_bcl+coreclr\EC2InstanceMetadata.cs:line 311
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.FetchCredentials() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+coreclr\DefaultInstanceProfileAWSCredentials.cs:line 142
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+coreclr\DefaultInstanceProfileAWSCredentials.cs:line 88
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+coreclr\DefaultInstanceProfileAWSCredentials.cs:line 106
   at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\CredentialsRetriever.cs:line 90
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\RetryHandler\RetryHandler.cs:line 137
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at test_dynamodb.Program.Main(String[] args) in G:\Test Projects\test-dynamodb\test-dynamodb\Program.cs:line 39

令人惊讶的是,它在Mac上运行良好!

3 个答案:

答案 0 :(得分:2)

如果您以某种方式跟踪堆栈,则会发现此异常不是由于SDK尝试连接到DynamoDB,而是由于EC2实例元数据URL(http://169.254.169.254)。 DynamoDB客户端正尝试从EC2检索IAM角色,并且在EC2之外运行时无法这样做。

我看到此异常从AWSSDK.Core版本3.3.24.6开始出现,而不是以前抛出的更有意义的异常:

Amazon.Runtime.AmazonServiceException : Failed to retrieve credentials from EC2 Instance Metadata Service.

但是,问题的根本原因是您尚未使AWS凭证可用于SDK。在适用于Visual Studio的AWS Explorer或create a credentials file中添加默认配置文件,您应该已启动并正在运行。

答案 1 :(得分:0)

我遇到了这个问题,我花了一段时间才找到它。

这是由于您的AWS凭证文件中没有正确的凭证引起的。

就我而言,我已覆盖[default]凭据,并且SDK无法正确进行身份验证。

一旦我解决了这个问题,它就会开始工作。

我正在使用AWSSDK.Lambda NuGet软件包版本3.3.102.46

答案 2 :(得分:0)

您可以简单地指定随机密钥来代替创建AWS配置文件:

var client = new AmazonDynamoDBClient(
    awsAccessKeyId: "test",
    awsSecretAccessKey: "test",
    clientConfig);

这绕过了配置文件检查,并且不使用访问密钥值。