Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何测试一个非常简单的AWS Lambda_C#_Amazon Web Services_Aws Lambda - Fatal编程技术网

C# 如何测试一个非常简单的AWS Lambda

C# 如何测试一个非常简单的AWS Lambda,c#,amazon-web-services,aws-lambda,C#,Amazon Web Services,Aws Lambda,我在VS.NET中创建了一个非常简单的lambda函数,并上传到AWS namespace HelloWorld { public class SayHi { public string FunctionHandler(string name, ILambdaContext context) { return name?.ToUpper(); } } } 当我用ING VS.NET测试它时,一切正常

我在VS.NET中创建了一个非常简单的lambda函数,并上传到AWS

namespace HelloWorld
{
    public class SayHi
    {
        public string FunctionHandler(string name, ILambdaContext context)
        {
            return name?.ToUpper();
        }
    }
}
当我用ING VS.NET测试它时,一切正常

但是如果我试图从AWS内部测试它,我就无法让它工作。 我使用以下json配置了一个测试事件:

{
  "name": "sample text"
}
但当我尝试运行此操作时,总是会出现错误:

{

  "errorType": "JsonSerializerException",
  "errorMessage": "Error converting the Lambda event JSON payload to a string. JSON strings must be quoted, for example \"Hello World\" in order to be converted to a string: The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.",
  "stackTrace": [
    "at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)",
    "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
  ],
  "cause": {
    "errorType": "JsonException",
    "errorMessage": "The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.",
    "stackTrace": [
      "at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)",
      "at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)",
      "at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)",
      "at System.Text.Json.JsonSerializer.ParseCore(ReadOnlySpan`1 utf8Json, Type returnType, JsonSerializerOptions options)",
      "at System.Text.Json.JsonSerializer.Deserialize[TValue](ReadOnlySpan`1 utf8Json, JsonSerializerOptions options)",
      "at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)"
    ],
    "cause": {
      "errorType": "InvalidOperationException",
      "errorMessage": "Cannot get the value of a token type 'StartObject' as a string.",
      "stackTrace": [
        "at System.Text.Json.Utf8JsonReader.GetString()",
        "at System.Text.Json.Serialization.Converters.JsonConverterString.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)",
        "at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(ReadStack& state, Utf8JsonReader& reader)",
        "at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)",
        "at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)"
      ]
    }
  }
}

这里缺少什么?

您可以更改输入参数的序列化程序(我建议不要这样做)。但默认情况下,c#lambda希望输入参数采用JSON格式

关于C#中的Lambda有一个很好的例子

根据您的示例,lambda方法的输入对象应该是类,而不是简单的字符串变量,第一个输入应该是如下所示的对象:

public class InputObject
{
   public string Name {get;set;}
}
namespace HelloWorld
{
    public class SayHi
    {
        public string FunctionHandler(InputObject inputObject, ILambdaContext context)
        {
            //your logic
        }
    }
}
您的方法输入应更改为以下内容:

public class InputObject
{
   public string Name {get;set;}
}
namespace HelloWorld
{
    public class SayHi
    {
        public string FunctionHandler(InputObject inputObject, ILambdaContext context)
        {
            //your logic
        }
    }
}

在Lambda控制台中进行测试时,需要输入一个普通字符串,而不是默认的json:

"sample text"

否则,默认lambda序列化程序不确定如何将json文档转换为字符串:


您使用一个名为“name”的JSON对象作为lambda函数的输入,但该函数接受一个字符串作为输入,而不是JSON对象。我理解这一点,但在AWS控制台上,当我尝试创建测试事件时,它会强制我创建JSON对象。我看不到其他选项,然后使用任何允许反序列化JSON对象的形式参数类型,例如Map/Dictionary或具有string/name属性的自定义类。好的,我明白你的意思。我将param更改为一个对象,然后可以使用标准JSON对象从控制台调用它。谢谢