windows - DynamoDb SocketException : A socket operation was attempted to an unreachable network

标签 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 上运行得很好!

最佳答案

如果您以某种方式追踪堆栈,您会发现此异常不是由于 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 凭证。在 AWS Explorer for Visual Studio 中添加默认配置文件,或 create a credentials file ,您应该已启动并运行。

关于windows - DynamoDb SocketException : A socket operation was attempted to an unreachable network,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53176573/

相关文章:

amazon-web-services - Amazon DynamoDB 查询其键包含子字符串的项目

python - 使用 python 持续更改 Windows 桌面墙纸

c# - .net core classlibrary调用.net framework类库

c# - 将中间件附加到 ASP.NET Core Web API 中的特定路由?

c# - .Net Core - IConfigurationRoot 读取整个 json 文件

python - AWS CloudFormation 堆栈创建不断失败

javascript - DynamoDB 中的正向和反向分页

windows - 如何在 Windows 中实现类似搜索的聚光灯?

c# - 如何在窗口中捕获数据

c++ - windows下如何正确使用共享内存