【问题标题】:Handling variable payloads with AWS Lambda使用 AWS Lambda 处理可变负载
【发布时间】:2022-08-11 20:12:14
【问题描述】:

我正在寻找在 C# 中构建一些 lambdas 来满足来自 Cognito 的某些事件。

我没有事件有效负载的类,所以我想我会创建一个通用接口并从那里工作。

因此,我将 lambda 的入口点定义为

using Amazon.Lambda.Core;
using Newtonsoft.Json.Linq;

// Assembly attribute to enable the Lambda function\'s JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace FCKM_PostAuth_01;

public class Function
{
    public string FunctionHandler(JObject request, ILambdaContext context)
    {
       // LambdaLogger.Log($\"JObject: {request.ToString()}\");

        return \"OK\";
    }
}

不幸的是,这在执行时失败(通过 AWS 测试工具),如下所示

System.Exception: Error deserializing the input JSON to type JObject
   at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.BuildParameters(ExecutionRequest request, ILambdaContext context) in C:\\codebuild\\tmp\\output\\src868358059\\src\\Tools\\LambdaTestTool\\src\\Amazon.Lambda.TestTool\\Runtime\\LambdaExecutor.cs:line 215
   at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.ExecuteAsync(ExecutionRequest request) in C:\\codebuild\\tmp\\output\\src868358059\\src\\Tools\\LambdaTestTool\\src\\Amazon.Lambda.TestTool\\Runtime\\LambdaExecutor.cs:line 52
---------------- Inner 1 Exception ------------
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.BuildParameters(ExecutionRequest request, ILambdaContext context) in C:\\codebuild\\tmp\\output\\src868358059\\src\\Tools\\LambdaTestTool\\src\\Amazon.Lambda.TestTool\\Runtime\\LambdaExecutor.cs:line 203
---------------- Inner 2 Exception ------------
Amazon.Lambda.Serialization.SystemTextJson.JsonSerializerException: Error converting the Lambda event JSON payload to type Newtonsoft.Json.Linq.JObject: The JSON value could not be converted to Newtonsoft.Json.Linq.JToken. Path: $.version | LineNumber: 1 | BytePositionInLine: 14.
   at Amazon.Lambda.Serialization.SystemTextJson.AbstractLambdaJsonSerializer.Deserialize[T](Stream requestStream)
---------------- Inner 3 Exception ------------
System.Text.Json.JsonException: The JSON value could not be converted to Newtonsoft.Json.Linq.JToken. Path: $.version | LineNumber: 1 | BytePositionInLine: 14.
   at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
   at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonDictionaryConverter`3.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TDictionary& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](ReadOnlySpan`1 utf8Json, JsonSerializerOptions options)
   at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.InternalDeserialize[T](Byte[] utf8Json)
   at Amazon.Lambda.Serialization.SystemTextJson.AbstractLambdaJsonSerializer.Deserialize[T](Stream requestStream)

    标签: c# aws-lambda


    【解决方案1】:

    显然,Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer 不能反序列化为JObject;为此,您应该改用Amazon.Lambda.Serialization.Json.JsonSerializer

    [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
    

    【讨论】:

      猜你喜欢
      • 2020-03-15
      • 1970-01-01
      • 2020-07-24
      • 2020-03-16
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      相关资源
      最近更新 更多