[英]DynamoDB UpdateItem not working when called via .NET api
I am using DynamoDb to set up a table which can be updated via API calls.我正在使用 DynamoDb 设置一个可以通过 API 调用更新的表。 After going through DynamoDB documentation, I have written an update item function in .NET as follows:
翻阅DynamoDB文档后,我在.NET中写了一个更新项function如下:
public static async Task IntAsync(string phone, string s)
{
getAsync(phone).Wait();
AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
clientConfig.RegionEndpoint = RegionEndpoint.APSouth1;
try
{
using (IAmazonDynamoDB ddbClient = new AmazonDynamoDBClient(clientConfig))
{
await ddbClient.UpdateItemAsync(new UpdateItemRequest
{
TableName = "TableName",
Key = new Dictionary<string, AttributeValue>
{
{"ID", new AttributeValue{S=phone} },
},
ExpressionAttributeNames = new Dictionary<string, string>()
{
{"#P", "Progress" },
{"#A", "attempt"+count.ToString() },
{"#S", s }
},
ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
{
{":s",new AttributeValue {S=DateTime.Now.ToString()}},
},
UpdateExpression = "SET #P.#A.#S = :s",
ConditionExpression = "attribute_not_exists(#P.#A.#S)"
});
}
}
catch (Exception e)
{
Console.WriteLine("Update Error: " + e.Message);
}
}
Issue: This function works fine when called via main and the changes are reflected instantaneously, however calling the same function via a.Net core Get api request does not return any result and continues saying sending request on postman.问题:当通过 main 调用时,此 function 工作正常并且更改会立即反映出来,但是通过 a.Net core 调用相同的 function Get api 请求不会返回任何结果,并继续说在 postman 上发送请求。
The Get Request is as follows:获取请求如下:
public async Task<string> GetAsync(string mobile, string s)
{
await Program.IntAsync(string mobile, string s);
return "Done";
}
However removing the function call from the get request results in the request going through and all the other functions are working perfectly.但是,从 get 请求中删除 function 调用会导致请求通过,并且所有其他功能都可以正常工作。 I have several other update requests carried out via get requests, all of them work perfectly.
我还有其他几个通过 get 请求执行的更新请求,它们都运行良好。
Welcome to the community BYNARI欢迎来到 BYNARI 社区
Your code looks good, except for not returning any value, I would change it to have an update response as UpdateItemAsync returns a response:) Change this您的代码看起来不错,除了不返回任何值外,我会将其更改为具有更新响应,因为 UpdateItemAsync 返回响应:)更改此
public static async Task IntAsync(string phone, string s)
{
//getAsync(phone).Wait();
AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
clientConfig.RegionEndpoint = RegionEndpoint.APSouth1;
try
{
using (IAmazonDynamoDB ddbClient = new AmazonDynamoDBClient(clientConfig))
{
await ddbClient.UpdateItemAsync(new UpdateItemRequest
To到
public static async Task IntAsync(string phone, string s)
{
//Added this variable
UpdateItemResponse updateResponse = null;
getAsync(phone).Wait();
AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
clientConfig.RegionEndpoint = RegionEndpoint.APSouth1;
try
{
using (IAmazonDynamoDB ddbClient = new AmazonDynamoDBClient(clientConfig))
{
//using the variable
updateResponse = await ddbClient.UpdateItemAsync(new UpdateItemRequest
***
At the end you should return the updateResponse if you would not do any transforming to it最后,如果您不对其进行任何转换,则应返回 updateResponse
and you'd of course check if not null then return your updateResponse to your "GetAsync" method你当然会检查如果不是 null 然后将你的 updateResponse 返回给你的“GetAsync”方法
Even in your GetAsync method you'd want to return your UpdateItemResponse即使在您的 GetAsync 方法中,您也想返回 UpdateItemResponse
so it would look something like:所以它看起来像:
public async Task<string> GetAsync(string mobile, string s)
{
var response = await IntAsync(string mobile, string s);
//Then you pack/unpack your response to get your attributes and
//convert them to string if that is what you need
var valueToReturn = CustomStringMethod(response.Attributes);
return valueToReturn;
}
Here is a link to the aws developer guide that might help you as well:) Update a record in DynamoDB这是 aws 开发人员指南的链接,它也可能对您有所帮助:) 更新 DynamoDB 中的记录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.