2

I have created a DynamoDb table in my dev machine and I'm trying to insert couple of rows from my .NET Core application using the CreateBatchWrite<T> method of DynamoDBContext object. I'm able to query the table from DynamoDB Javascript Shell window from the localhost:8000/shell url and it returns row count as 0. But when trying to call the CreateBatchWrite<T> method I get the error, "Entity doesn't exist in AsyncLocal".

Romy Mathews
  • 797
  • 7
  • 13

3 Answers3

5

Add this line before any DynamoDB API methods are executed:

AWSXRayRecorder.Instance.ContextMissingStrategy = ContextMissingStrategy.LOG_ERROR;

You can find more info in GitHub discussion https://github.com/aws/aws-xray-sdk-dotnet/issues/69#issuecomment-482688754

Also, you will need to import these 2 packages.

using Amazon.XRay.Recorder.Core;
using Amazon.XRay.Recorder.Core.Strategies;
Aleksey Cherenkov
  • 1,405
  • 21
  • 25
Savan Thakkar
  • 121
  • 1
  • 7
  • @HannaHolasava Glad that it helped. Note that this line should be added only during development as per XRay dev guideline: https://docs.amazonaws.cn/en_us/xray/latest/devguide/xray-guide.pdf – Savan Thakkar Jul 23 '21 at 20:49
  • Thank you, I missed that too and this line worked for me. – KateMak Mar 24 '23 at 20:49
5

Explanation

When using X-Ray, this happens when there is an attempt to create a SubSegment without a Parent Segment. Depending on your setup, when you run a query it might try creating a SubSegment, but it's failing because there is no parent segment.

This is common when running a Lambda function locally, as the Mock Lambda Test Tool will not create a Segment for you like the actual Lambda environment does on AWS. This can happen in other scenarios too.

More details here: https://github.com/aws/aws-xray-sdk-dotnet/issues/125

Solution

Easiest way to solve this is disabling X-Ray locally (as you probably don't want to generate traces locally):

In appsettings.Development.json add this:

"XRay": {
  "DisableXRayTracing": "true",
  "UseRuntimeErrors": "false",
  "CollectSqlQueries": "false"
}

The important bit is the DisableXRayTracing equals true.

Make sure your appsettings.Development.json is set to Copy Always in the properties window. You can do this by including this in your .csproj:

<ItemGroup>
  <None Update="appsettings.Development.json">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
  <None Update="appsettings.json">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup>

If you really want to trace things locally, then make sure you create a parent segment only when running locally (on AWS this would cause problems as you would have two parent segments, one created manually by you, another one created by AWS).

Alisson Reinaldo Silva
  • 10,009
  • 5
  • 65
  • 83
  • 1
    This is not working for me in VS 2022 17.4.1 and a net6 minimal api lambda. I ended up addig `#if !DEBUG AWSSDKHandler.RegisterXRayForAllServices(); #endif` instead. – JoanComasFdz Dec 01 '22 at 23:18
1

If you are tracing requests made with the AWS SDK, the X-Ray SDK attempts to generate a subsegment automatically to represent those requests, such as CreateBatchWrite. However, a subsegment can only be created as the child of an existing Segment, so if you have not created a segment beforehand that Entity doesn't exist error will occur.

See these docs for how to create custom segments. Alternatively, if you are developing a web app, the X-Ray SDK can automatically create segments for requests made to your service by adding configuration described in these docs

William Armiros
  • 267
  • 1
  • 10