aws-sdk-net: AWS SDK - Intermittent NullReferenceException

Describe the bug

When attempting to insert records using IDynamoDBContext.SaveAsync a NullReferenceException is thrown from the AWS DynamoDB SDK.

Expected Behavior

The IDynamoDBContext.SaveAsync call is successful and the record is created within the configured AWS DynamoDB table.

Current Behavior

The following stack trace shows where the error is raised:

innerExceptionMesage: Object reference not set to an instance of an object. innerExceptionStackTrace: at Amazon.DynamoDBv2.DocumentModel.Table.LoadTable(IAmazonDynamoDB ddbClient, TableConfig config) at Amazon.DynamoDBv2.DataModel.DynamoDBContext.GetUnconfiguredTable(String tableName, Boolean disableFetchingTableMetadata) at Amazon.DynamoDBv2.DataModel.DynamoDBContext.GetTargetTable(ItemStorageConfig storageConfig, DynamoDBFlatConfig flatConfig, DynamoDBConsumer consumer) at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveHelperAsync(Type valueType, Object value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken) at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveHelperAsync[T](T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken) at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveAsync[T](T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)

Reproduction Steps

Unable to reproduce locally, only where hosted.

Configuration of AWS SDK services (AWSSDK.Extensions.NETCore.Setup)

var options = configuration.GetAWSOptions();
services.AddDefaultAWSOptions(options);
services.AddAWSService<IAmazonDynamoDB>();
services.TryAddSingleton<IDynamoDBContext, DynamoDBContext>();

Saving the record

var operationConfig = new DynamoDBOperationConfig { OverrideTableName = _dynamoTableName };
await _dynamoDbContext.SaveAsync(recordToSave, operationConfig);

Note: _dynamoTableName is the logical id for the DynamoDB table.

Additionally, we use the following logic within Startup.cs to ensure that the required interfaces are registered and available before marking the application as ready (letting AWS restart the task if necessary):

private static void VerifyAmazonConnectivity(IApplicationBuilder app)
{
        app.ApplicationServices.GetRequiredService<IAmazonDynamoDB>();
        app.ApplicationServices.GetRequiredService<IDynamoDBContext>();
}

Possible Solution

We have been unable to reproduce the issue locally, and so instead must resort to failing and creating new Fargate instances until we see evidence of the IDynamoDBContext.SaveAsync working as intended.

Additional Information/Context

Initially we manually registered the required dependencies as follows:

services.AddSingleton<IAmazonDynamoDB, AmazonDynamoDBClient>();
services.AddSingleton<IDynamoDBContext, DynamoDBContext>();

This resulted in a different error message when invoking IDynamoDBContext.SaveAsync:

innerExceptionMesage: Value cannot be null. (Parameter ‘ddbClient’) innerExceptionStackTrace: at Amazon.DynamoDBv2.DocumentModel.Table…ctor(IAmazonDynamoDB ddbClient, TableConfig config) at Amazon.DynamoDBv2.DocumentModel.Table.LoadTable(IAmazonDynamoDB ddbClient, TableConfig config) at Amazon.DynamoDBv2.DataModel.DynamoDBContext.GetUnconfiguredTable(String tableName, Boolean disableFetchingTableMetadata) at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveHelperAsync(Type valueType, Object value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken) at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveHelperAsync[T](T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)

As you can see from the message there’s an implication that the required client isn’t being injected. Our representative example purposefully validates they are available before allowing the task to respond to requests.

This in turn changes the error message from the above to the one reported within this issue.

Relatedely, we have other applications using the manual registration and are able to save records using the same functionality without issue.

AWS .NET SDK and/or Package version used

AWSSDK.Extensions.NETCore.Setup : 3.7.301 AWSSDK.DynamoDBv2 : 3.7.405.7 AWSSDK.Core : 3.7.401.1

Targeted .NET Platform

.NET 8

Operating System and version

Alpine Linux (ASP NET Docker Image)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I haven’t been able to reproduce it locally, with the using statement or without the using statement. But does removing the using statement fix the issue for you?

Yes, we’ve not seen an issue since removing those statements - thanks all for your support 🙇

Hello, reading through this issue right now. Thank you for providing reproduction steps above. We’ve also received a different customer report internally. I will post any updates I find here as well