如何解决.NET控制台/库应用程序中的 "Entity doesn't exist in callcontext" 异常?

0

【以下的问题经过翻译处理】 我试图将AWS X-Ray添加到我们的解决方案中,包括S3、Lambda和DynamoDB服务,但我在处理异常"Entity doesn't exist in callcontext"方面遇到了困难。

通常情况下,我需要在通过WCF Web服务文件(.svc)启动的库项目中使用X-Ray服务,但为了解决问题,我在一个简单的控制台应用程序中尝试了以下最小示例代码,并且出现了完全相同的错误:

private static void Main(string[] args)
{
    AWSSDKHandler.RegisterXRayForAllServices();

    var lambdaClient = new AmazonLambdaClient(awsCredentials, regionEndpoint);
    lambdaClient.Invoke(new InvokeRequest { ... });
}

当禁用RegisterXRayForAllServices行时,Lambda请求可以正常工作,但当启用该行时,在调用Lambda请求时会引发以下异常:

at Amazon.XRay.Recorder.Core.Internal.Context.CallContextContainer.GetEntity()
   at Amazon.XRay.Recorder.Handlers.AwsSdk.Internal.XRayPipelineHandler.ProcessBeginRequest(IExecutionContext executionContext)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Amazon.XRay.Recorder.Handlers.AwsSdk.Internal.XRayPipelineHandler.ProcessBeginRequest(IExecutionContext executionContext)
   at Amazon.XRay.Recorder.Handlers.AwsSdk.Internal.XRayPipelineHandler.InvokeSync(IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeSync(IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\CallbackHandler.cs:line 46
   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeSync(IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\ErrorCallbackHandler.cs:line 44
   at Amazon.Runtime.Internal.MetricsHandler.InvokeSync(IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\MetricsHandler.cs:line 40
   at Amazon.Runtime.Internal.RuntimePipeline.InvokeSync(IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\RuntimePipeline.cs:line 136
   at Amazon.Runtime.AmazonServiceClient.Invoke[TResponse](AmazonWebServiceRequest request, InvokeOptionsBase options) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AmazonServiceClient.cs:line 206
   at Program.Main(String[] args) in Program.cs:line 42

使用中的版本

.Net Framework 4.7.2
Nuget AWSDDK.Lambda 3.7.13.16
Nuget AWSXRayRecoder: 2.10.1

在其他不涉及AWS服务的项目中,我没有在RegisterXRay(this, "Program")和为WCF服务添加分段和自定义端点行为时出现问题,但在这里,我需要一些帮助,如何正确设置XRay recorder以用于AWS服务。在App.config/Web.config中是否需要额外的配置?

profile picture
专家
已提问 5 个月前34 查看次数
1 回答
0

【以下的回答经过翻译处理】 使用 AWSSDKHandler.RegisterXRayForAllServices() 将检测用户应用程序中的所有 AWS SDK 客户端。这意味着 X-Ray SDK 将为这些客户端发出的每个请求创建一个子段。

在X-Ray SDK中,只有在已经打开了父段或子段的情况下才能创建一个子段,但我在提供的代码示例中没有看到这一点。

你可以尝试创建一个段(Segment)并查看问题是否仍然存在。更新后的代码应如下所示:

private static void Main(string[] args)
{

AWSSDKHandler.RegisterXRayForAllServices();
AWSXRayRecorder.Instance.BeginSegment("MainSegment");
var lambdaClient = new AmazonLambdaClient(awsCredentials, regionEndpoint);
lambdaClient.Invoke(new InvokeRequest { ... });
AWSXRayRecorder.Instance.EndSegment();

}
profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则