c# - 应用程序从本地 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 访问的。
为什么我的应用程序看到错误/不同的表格列表?
我尝试过的其他一些事情无济于事:
- 核对/重新安装 DynamoDB
- 重新启动我的电脑
- 从今天早些时候恢复到我的代码的先前版本
CREATED_IN_CODE
在不同区域的AWS中寻找表
解决方案
上面的代码对我来说很好。有时您需要确保有一个区域集,即使您正在设置一个ServiceUrl
.
推荐阅读
- vnc - 有没有办法将 VNC 连接限制为 TrueNAS 虚拟机中的一个用户?
- extjs - ExtJS 7.2 动态更改整个应用程序“字体系列”
- node.js - 猫鼬错误:“typeof globalThis”类型上不存在属性“缓冲区”
- javascript - 如何从 Firebase 实时数据库中删除一些数据?
- rust - 我应该如何处理这个生锈的举动?
- java - 即使 Uri 存在,file.exists() 也会返回 false,因此无法删除文件
- laravel - Laravel 合并集合并按值排序
- html - 反应生产代码中的奇怪 CSS 行为
- vba - VBA - 宏 - 使用“Microsoft Print to PDF”打印机打印多个数字签名的 PDF 文件并将其保存在子文件夹中
- c# - 如何将值四舍五入到数组中最接近的数字?