【问题标题】:DynamoDB UpdateItem not working when called via .NET api通过 .NET api 调用 DynamoDB UpdateItem 时不起作用
【发布时间】:2020-07-27 04:11:26
【问题描述】:

我正在使用 DynamoDb 设置一个可以通过 API 调用更新的表。在浏览了DynamoDB文档之后,我在.NET中写了一个更新项功能如下:

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);
        }
    }

问题: 此函数在通过 main 调用时工作正常,并且更改会立即反映,但是通过 .Net core Get api 请求调用相同的函数不会返回任何结果并继续在邮递员上发送请求。

获取请求如下:

 public async Task<string> GetAsync(string mobile, string s)
 { 
    await Program.IntAsync(string mobile, string s); 
    return "Done";
 }

但是,从 get 请求中删除函数调用会导致请求通过,并且所有其他函数都可以正常工作。我还有其他几个通过 get 请求执行的更新请求,它们都运行良好。

这是邮递员一直在说的

【问题讨论】:

    标签: c# .net-core amazon-dynamodb


    【解决方案1】:

    欢迎来到社区 BYNARI
    您的代码看起来不错,除了不返回任何值外,我会将其更改为具有更新响应,因为 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
    

    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
    ***
    

    最后你应该返回 updateResponse,如果你不想对其进行任何转换

    您当然会检查是否不为空,然后将您的 updateResponse 返回到您的“GetAsync”方法
    即使在您的 GetAsync 方法中,您也希望返回您的 UpdateItemResponse

    所以它看起来像:

     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;
      }
    

    这里是 aws 开发人员指南的链接,它也可能对您有所帮助 :) Update a record in DynamoDB

    【讨论】:

    • 感谢您的建议,我进行了必要的更改,但请求仍未发送。
    • 确定任何时候,好的@BYNARI当你写请求时没有被发送,这是什么意思?没有调用 GetAsync 方法?
    • 请检查编辑,这就是我说请求未发送时的意思。谢谢
    • 好吧,我可能错过了,getAsync(phone).Wait() 在那里做什么?它可能是保留函数的方法,因为它正在等待它而没有任何运行或结果。尝试将其注释掉并尝试再次运行您的方法
    • getAsync(phone).Wait() 更新用于指定条目号的全局变量计数,注释该行似乎可以解决问题,感谢您的所有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2019-01-15
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多