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

.NET Core集成DynamoDB遇AmazonDynamoDBClient方法访问权限错误

Fixing Error CS0122 with AmazonDynamoDBClient in .NET Core

Hey there! Let's break down why you're running into that CS0122 "inaccessible due to protection level" error with DynamoDB, and how to fix it quickly.

Root Cause

The official example you're referencing is built on AWS SDK for .NET v2, but you're almost certainly using the newer v3 SDK (the default for modern .NET Core projects). In v3, the synchronous methods like PutItem(PutItemRequest) and GetItem(GetItemRequest) have been marked as internal—meaning they're no longer accessible from your code. AWS shifted to prioritizing asynchronous operations in v3 to align with cloud best practices.

The easiest and most idiomatic fix is to switch to the asynchronous equivalents, which are fully public and supported. Here's a quick example of creating an item with PutItemAsync:

using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;

var dynamoDbClient = new AmazonDynamoDBClient();

var putRequest = new PutItemRequest
{
    TableName = "YourTableName",
    Item = new Dictionary<string, AttributeValue>
    {
        { "UserId", new AttributeValue { S = "user_123" } },
        { "UserName", new AttributeValue { S = "Jane Doe" } }
    }
};

// Use await to call the async method
await dynamoDbClient.PutItemAsync(putRequest);

Make sure your calling method is marked with the async keyword to use await.

Solution 2: Sync Operation (If Absolutely Necessary)

If you have a specific need for synchronous execution, you can use the GetSync() extension method from the Amazon.Runtime namespace to wrap the async call:

using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.Runtime; // Required for GetSync()

var dynamoDbClient = new AmazonDynamoDBClient();
var putRequest = new PutItemRequest { /* Your request details */ };

// Convert async to sync (not recommended for production)
dynamoDbClient.PutItemAsync(putRequest).GetSync();

Note: AWS discourages this approach for production code, as it can lead to thread blocking and reduced performance in cloud environments.

Quick Check: Verify NuGet Package Version

Double-check that you have the latest AWSSDK.DynamoDBv2 NuGet package installed (version 3.x+). You can confirm this in your project's NuGet Package Manager or by checking your .csproj file:

<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.x.x" />

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:37:16