首页 > 解决方案 > 应用程序从本地 DynamoDB 实例中看到零个/不同的表

问题描述

我正在构建一个 WebAPI 项目。我已下载 DynamoDB 并使用-inMemory. 创建几个表后,我在本地运行此命令:aws dynamodb list-tables --endpoint-url http://localhost:8000,结果是:

{
    "TableNames": [
        "Users",
        "Tmp"
    ]
}

当我运行我的应用程序时,我创建了一个客户端并查询表:

using (var client = DatabaseClientFactory.CreateClient())
            {
                // debugging
                var temp = await client.ListTablesAsync();
                return $"{temp.TableNames.Count} tables: " + string.Join(", ", temp.TableNames);
            }

这返回0 tables:

DB 客户端是此入门代码的轻微修改版本:

using System;
using System.Net;
using System.Net.NetworkInformation;
using Amazon;
using Amazon.DynamoDBv2;

namespace Database
{
    // Adapted from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NET.01.html
    public static class DatabaseClientFactory
    {
        /*-----------------------------------------------------------------------------------
        *  If you are creating a client for the Amazon DynamoDB service, make sure your credentials
        *  are set up first, as explained in:
        *  https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SettingUp.DynamoWebService.html,
        *
        *  If you are creating a client for DynamoDBLocal (for testing purposes),
        *  DynamoDB-Local should be started first. For most simple testing, you can keep
        *  data in memory only, without writing anything to disk.  To do this, use the
        *  following command line:
        *
        *    java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory
        *
        *  For information about DynamoDBLocal, see:
        *  https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html.
        *-----------------------------------------------------------------------------------*/

        private const int Port = 8000;

        public static AmazonDynamoDBClient CreateClient()
        {
            // If this line throws, you need to download the AWS CLI, run `aws configure` and specify your access key.
            var client = new AmazonDynamoDBClient();
            var dynamoDbConfig = new AmazonDynamoDBConfig();

            // First, check to see whether anyone is listening on the DynamoDB local port
            // (by default, this is port 8000, so if you are using a different port, modify this accordingly)
            var portAvailable = IsPortAvailable();

            if (portAvailable)
            {
                Console.WriteLine("  -- The local instance of DynamoDB is NOT running. Using PROD.");
                // TODO: this should come out of appSettings.Production.json
                dynamoDbConfig.RegionEndpoint = RegionEndpoint.USEast2;
            }
            else
            {
                // Local address ignores AWS credentials
                Console.WriteLine("  -- The local instance of DynamoDB is running!");
                dynamoDbConfig.ServiceURL = $"http://localhost:{Port}";
            }

            client = new AmazonDynamoDBClient(dynamoDbConfig);

            return client;
        }

        private static bool IsPortAvailable()
        {
            // Evaluate current system TCP connections. This is the same information provided
            // by the netstat command line application, just in .Net strongly-typed object
            // form.  We will look through the list, and if our port we would like to use
            // in our TcpClient is occupied, we will set isAvailable to false.
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpListeners();

            foreach (IPEndPoint endpoint in tcpConnInfoArray)
            {
                if (endpoint.Port == Port)
                {
                    return false;
                }
            }

            return true;
        }
    }
}

这很奇怪 - 我的两张桌子没有出现。

更奇怪的是:如果我这样调用client.CreateTableAsync,他们成功创建了一个表,而我的 API 调用的返回值变成了1 tables: CREATED_IN_CODE

似乎我在同一个端口上本地运行了两个版本的 DynamoDB——一个是应用程序访问的,一个是 CLI 访问的。

为什么我的应用程序看到错误/不同的表格列表?

我尝试过的其他一些事情无济于事:

标签: c#amazon-dynamodbasp.net-core-webapiasp.net-core-3.1

解决方案


上面的代码对我来说很好。有时您需要确保有一个区域集,即使您正在设置一个ServiceUrl.


推荐阅读