ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#-在步骤函数中将Json字符串传递给AWS Lambda-JsonReaderException错误

2019-11-09 02:05:34  阅读:15  来源: 互联网

标签:net-core aws-lambda aws-step-functions c



我正在尝试在Step Function中使用AWS Lambda函数.当Lambda函数经过单独测试并且json输入被转义时,它可以正常工作.但是,当输入通过步进函数传递给lambda函数时,我收到了JsonReaderException错误.我究竟做错了什么?社区是否知道解决此问题的方法?

lambda函数:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Formatters.Binary;
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.Json.JsonSerializer))]
namespace AWSLambda1
{
    public class Function
    {
        public void PostsBasedOnOddOrEven(string input, ILambdaContext context)
        {
            var details = JObject.Parse(input);
            var postId = (int) details["id"];
            var oddOrEvenResult = (int) details["OddOrEvenPostsResult"];
        }
    }
}

从Step Function输入到Lambda函数:

{
  "id": "1",
  "OddOrEvenPostsResult": 2
}

输入Lambda函数(可通过Visual Studio调用):

"{ \"id\": \"1\", \"OddOrEvenPostsResult\": 2}"

异常堆栈跟踪:

{
  "errorType": "JsonReaderException",
  "errorMessage": "Unexpected character encountered while parsing value: {. Path '', line 1, position 1.",
  "stackTrace": [
    "at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
    "at Newtonsoft.Json.JsonTextReader.ReadAsString()",
    "at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)",
    "at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
    "at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)",
    "at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
    "at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream requestStream)",
    "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
  ]
}

Lambda函数作为Step Function的一部分时不起作用

Lambda Function not working when it is part of Step Function

Lambda函数在单独测试时正常工作

Lambda Function working when tested individually

解决方法:

由于您的lambda函数期望输入为字符串,因此框架尝试将输入解析为字符串.

但是,输入实际上是JSON对象,而不是字符串.

因此,解析器将失败,并显示“意外字符”错误.解析器期望一个“字符,它将指示字符串的开头.

因此,这是解决方法:

>声明一个代表输入的c#类

public class FunctionInput
{
    public int id { get; set; }
    public int OddOrEvenPostsResult { get; set; }
}

>更改函数签名,以期望输入FunctionInput类型

public class Function
{    
    public void PostsBasedOnOddOrEven(FunctionInput input, ILambdaContext context)
    {
        var postId = input.id;
        var oddOrEvenResult = input.OddOrEvenPostsResult;
    }
}

注意:您不需要自己解析输入.



标签:net-core,aws-lambda,aws-step-functions,c

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有