You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决DynamoDB报错:参数2无法从DynamoDBOperationConfig转为AmazonDynamoDBCallback

问题背景

我正在使用DynamoDB进行开发,项目中需要动态设置数据库表名并动态加载数据库,以下是我的代码:

using UnityEngine;
using System.Collections;
using Amazon.DynamoDBv2.DataModel;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
using Amazon.DynamoDBv2;
using UnityEngine.UI;
using Amazon;

namespace AWSSDK.Examples
{
public class HighLevel3 : DynamoDbBaseExample
{

    private IAmazonDynamoDB _client;
    private DynamoDBContext _context;

    public Text resultText;
    public Button back;
    public Button createOperation;
    public Button updateOperation;
    public Button deleteOperation;
    public string S_tablefieldset;
    string bookID;
    int bookID1 = 9;
    public string Email;

    private DynamoDBContext Context
    {
        get
        {
            if (_context == null)
                _context = new DynamoDBContext(_client);

            return _context;
        }
    }

    void Awake()
    {
        
        back.onClick.AddListener(BackListener);
        createOperation.onClick.AddListener(PerformCreateOperation);
        _client = Client;
        S_tablefieldset = "Orders";
        HighLevelTableExample.GetDynamoDbOperationConfig(S_tablefieldset);

    }

    void Start()
    {
        bookID = SystemInfo.deviceUniqueIdentifier;
        System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "Orders");
        PerformCreateOperation();
    }


    public static DynamoDBOperationConfig GetDynamoDbOperationConfig(string dynamoDbTable)
    {
        Debug.Log("The Table name is " + dynamoDbTable);
        var tableName = System.Environment.GetEnvironmentVariable("MONO_REFLECTION_SERIALIZER");
        Debug.Log("The Table name is" + tableName);
        DynamoDBOperationConfig config = new DynamoDBOperationConfig()
        {
            OverrideTableName = tableName
        };

        return config;
    }

      public void PerformCreateOperation()
    {
        Debug.Log(" I am in Perform Create Operation is working fine and good");
        Book12 myBook = new Book12
        {
            OrderID = bookID,
            OrderItem = 50,
           
        };

        // Save the book.
        var tableName = System.Environment.GetEnvironmentVariable("TABLE_NAME");
        Context.SaveAsync(myBook, new DynamoDBOperationConfig {

      OverrideTableName = tableName });
                
    }
}
public class Book12
{
    [DynamoDBHashKey]   // Hash key.
    public string  OrderID { get; set; }
    [DynamoDBProperty]
    public string UserName { get; set; }
    [DynamoDBProperty]
    public int OrderItem { get; set; }
}
报错信息

error CS1503: Argument 2: cannot convert from 'Amazon.DynamoDBv2.DataModel.DynamoDBOperationConfig' to Amazon.DynamoDBv2.AmazonDynamoDBCallback'

解决方案

错误原因是SaveAsync方法的参数顺序传错:AWS .NET SDK中DynamoDBContext.SaveAsync的非await重载,第二个参数要求传入回调委托,你直接把DynamoDBOperationConfig放在第二个参数位,类型不匹配触发报错。同时你的代码存在隐性问题:Start方法中给MONO_REFLECTION_SERIALIZER环境变量赋值,但是读取时用的环境变量名是TABLE_NAME,会读不到有效值。

修复步骤如下:

  1. 先统一环境变量名,修改Start方法中的赋值逻辑:
void Start()
{
    bookID = SystemInfo.deviceUniqueIdentifier;
    // 统一环境变量名,和读取时保持一致
    System.Environment.SetEnvironmentVariable("TABLE_NAME", "Orders");
    PerformCreateOperation();
}
  1. 修正SaveAsync的传参,根据需求选择以下任意一种写法:
  • 不需要回调的写法,第二个参数传null即可:
Context.SaveAsync(myBook, null, new DynamoDBOperationConfig 
{
    OverrideTableName = tableName 
});
  • 需要监听保存结果的写法,传入对应回调:
Context.SaveAsync(myBook, (AmazonDynamoDBResult<object> result) => 
{
    if(result.Exception != null)
    {
        Debug.LogError("数据保存失败:" + result.Exception.Message);
        return;
    }
    Debug.Log("数据保存成功");
}, new DynamoDBOperationConfig 
{
    OverrideTableName = tableName 
});
  • 若你的SDK版本支持Task异步,可直接用await写法,不用额外传回调:
// 方法标记为async
public async void PerformCreateOperation()
{
    // 原有逻辑不变
    await Context.SaveAsync(myBook, new DynamoDBOperationConfig 
    {
        OverrideTableName = tableName 
    });
}

内容的提问来源于stack exchange,提问作者karthik keyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 08:06:05