应用程序从本地 DynamoDB 实例中看到 zero/different 个表
Application sees zero/different tables from what local DynamoDB instance has
我正在构建一个 WebAPI 项目。我下载了 DynamoDB 并在本地 运行 连接 -inMemory
。在创建了几个 table 之后,我在本地 运行 这个命令:aws dynamodb list-tables --endpoint-url http://localhost:8000
,结果是:
{
"TableNames": [
"Users",
"Tmp"
]
}
当我 运行 我的应用程序时,我创建了一个客户端,并查询 tables:
using (var client = DatabaseClientFactory.CreateClient())
{
// debugging
var temp = await client.ListTablesAsync();
return $"{temp.TableNames.Count} tables: " + string.Join(", ", temp.TableNames);
}
这个returns 0 tables:
数据库客户端是 this starter code 的轻微修改版本:
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;
}
}
}
真奇怪 - 我的两个 table 没有出现。
更奇怪:如果我像client.CreateTableAsync
那样调用,他们成功地创建了一个table,并且我的API调用的return值变成了1 tables: CREATED_IN_CODE
我好像有两个版本的 DynamoDB 运行在同一个端口本地连接 - 一个应用程序访问,一个 CLI 访问。
为什么我的应用会看到 wrong/different 个 table 列表?
其他一些我尝试过但没有用的东西:
- Nuking/reinstalling DynamoDB
- 正在重新启动我的电脑
- 恢复到我今天早些时候的代码版本
- 在不同地区的AWS中寻找
CREATED_IN_CODE
table
上面的代码对我来说效果很好。有时您需要确定是否设置了区域,即使您设置的是 ServiceUrl
.
我正在构建一个 WebAPI 项目。我下载了 DynamoDB 并在本地 运行 连接 -inMemory
。在创建了几个 table 之后,我在本地 运行 这个命令:aws dynamodb list-tables --endpoint-url http://localhost:8000
,结果是:
{
"TableNames": [
"Users",
"Tmp"
]
}
当我 运行 我的应用程序时,我创建了一个客户端,并查询 tables:
using (var client = DatabaseClientFactory.CreateClient())
{
// debugging
var temp = await client.ListTablesAsync();
return $"{temp.TableNames.Count} tables: " + string.Join(", ", temp.TableNames);
}
这个returns 0 tables:
数据库客户端是 this starter code 的轻微修改版本:
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;
}
}
}
真奇怪 - 我的两个 table 没有出现。
更奇怪:如果我像client.CreateTableAsync
那样调用,他们成功地创建了一个table,并且我的API调用的return值变成了1 tables: CREATED_IN_CODE
我好像有两个版本的 DynamoDB 运行在同一个端口本地连接 - 一个应用程序访问,一个 CLI 访问。
为什么我的应用会看到 wrong/different 个 table 列表?
其他一些我尝试过但没有用的东西:
- Nuking/reinstalling DynamoDB
- 正在重新启动我的电脑
- 恢复到我今天早些时候的代码版本
- 在不同地区的AWS中寻找
CREATED_IN_CODE
table
上面的代码对我来说效果很好。有时您需要确定是否设置了区域,即使您设置的是 ServiceUrl
.