简体   繁体   English

尝试调用 CreateBatchWrite 时出现错误,“AsyncLocal 中不存在实体”<t> DynamoDBContext object 的方法</t>

[英]Getting error, “Entity doesn't exist in AsyncLocal” when trying to call CreateBatchWrite<T> method of DynamoDBContext object

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.我在我的开发机器中创建了一个 DynamoDb 表,我正在尝试使用 DynamoDBContext object 的CreateBatchWrite<T>方法从我的DynamoDBContext核心应用程序中插入几行。 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 ". 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存在于 AsyncLocal 中

Add this line before any DynamoDB API methods are executed:在执行任何 DynamoDB API 方法之前添加此行:

AWSXRayRecorder.Instance.ContextMissingStrategy = ContextMissingStrategy.LOG_ERROR; AWSXRayRecorder.Instance.ContextMissingStrategy = ContextMissingStrategy.LOG_ERROR;

You will need to import these 2 packages.您将需要导入这两个包。 //using Amazon.XRay.Recorder.Core; //使用 Amazon.XRay.Recorder.Core; //using Amazon.XRay.Recorder.Core.Strategies; //使用 Amazon.XRay.Recorder.Core.Strategies;

Explanation解释

When using X-Ray, this happens when there is an attempt to create a SubSegment without a Parent Segment .使用 X-Ray 时,当尝试创建没有Parent SegmentSubSegment时会发生这种情况。 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.这在本地运行 Lambda function 时很常见,因为模拟 Lambda 测试工具不会像实际 Z04A7DA3C5B048CADB8 上的实际 Z04A7DA3C5B8BZ 环境那样为您创建 This can happen in other scenarios too.这也可能发生在其他场景中。

More details here: https://github.com/aws/aws-xray-sdk-dotnet/issues/125更多细节在这里: 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):解决此问题的最简单方法是在本地禁用 X-Ray(因为您可能不想在本地生成跟踪):

In appsettings.Development.json add this:appsettings.Development.json添加这个:

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

The important bit is the DisableXRayTracing equals true.重要的是DisableXRayTracing等于 true。

Make sure your appsettings.Development.json is set to Copy Always in the properties window.确保您的 appsettings.Development.json 在属性appsettings.Development.json中设置为始终复制 You can do this by including this in your .csproj :您可以通过将其包含在.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).如果您真的想在本地跟踪事物,请确保仅在本地运行时创建父段(在 AWS 上,这会导致问题,因为您将有两个父段,一个由您手动创建,另一个由 AWS 创建)。

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 .如果您正在跟踪使用 AWS SDK 发出的请求,X-Ray SDK 会尝试自动生成一个子段来表示这些请求,例如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.但是,子段只能创建为现有段的子段,因此如果您没有事先创建段, Entity doesn't exist将发生错误。

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或者,如果您正在开发 web 应用程序,X-Ray SDK 可以通过添加这些文档中描述的配置来自动为向您的服务发出的请求创建分段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当对象不存在时,DynamoDBMapper才会保存 - DynamoDBMapper save only if object doesn't exist 当 object 不存在时,使用 terraform 无法安全读取 s3_bucket_object - Fail safe reading s3_bucket_object with terraform when object doesn't exist Aws XRay .net控制台应用程序中的“实体在CallContext中不存在” - Aws XRay “Entity doesn't exist in CallContext” in .net console application 在AWS上部署时,MySQL表不存在 - MySQL Table doesn't exist when deployed on AWS 当从CF源请求的对象不存在时,s3不遵循重定向规则(而是拒绝访问) - When requested object from CF origin doesn't exist, s3 not following redirection rules (Access Denied instead) DynamoDBContext没有Save()方法 - DynamoDBContext does not have Save() method 实例ID不存在 - Instance ID doesn't exist 仅当实体尚不存在时,是否可以在 CloudFormation 中设置属性? - Can I set a property in CloudFormation only if the entity doesn't exist yet? X-Ray 跟踪不显示内部方法调用 - X-Ray trace doesn't shows inner method call Boto3 / AWS:创建实例时“ ImageId不存在” - Boto3/AWS: “ImageId doesn't exist” when creating Instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM