我试图在C#中编写AWS Lambda函数。我有AWS Toolkit for Visual Studio 2015.我使用AWS Lambda项目(.Net Core)创建了一个项目,然后选择了Empty Function选项。这给了我以下代码:
更新和答案02/24/17 - 标记为答案的评论是有用的知识,但不是我的实际答案。 @ PavelSafronov在那个答案中的评论就是这个伎俩。我要么一无所知(并得到错误),或者我认为它希望我以JSON格式提供信息,所以我会输入{ "input": "Some string" }
并仍然得到错误。现在我刚刚传入“Some string”就可以了。但是,就我而言,这似乎是一个错误。 string
默认是nullable
,亚马逊编写的代码甚至假设它可能是input?.ToUpper()
正在检查?.
的null
的虚拟。
注意:我添加了LambdaLogger.Log
线和Constructor
以查看我的位置:
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.Json;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(JsonSerializer))]
namespace AWSLambdaTest1 {
public class Function {
public Function() {
LambdaLogger.Log("Within the Constructor");
}
public string KevinsTestFunction(string input, ILambdaContext context) {
LambdaLogger.Log("Within the KTF");
return input?.ToUpper();
}
}
}
输出屏幕和解决方案资源管理器说:
Errors in C:\Test Projects\AWSLambda1\AWSLambda1\AWSLambda1.xproj
Unable to resolve 'Amazon.Lambda.Core (>= 1.0.0)' for '.NETCoreApp,Version=v1.0'.
但是,这将构建并发布到AWS。我甚至可以调用它返回以下内容 - 这是我的主要问题:
{
"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 lambda_method(Closure , Stream , Stream , ContextInfo )"
]
}
日志文件显示Constructors
日志消息到达那里但不是实际的KevingsTestFunction
日志消息。
在旁注中,我可以通过将以下内容添加到我的Unable to resolve 'Amazon.Lambda.Core (>= 1.0.0)' for '.NETCoreApp,Version=v1.0'
文件中来获取project.json
错误:
"runtimes": {
"win10-x64": {},
"win81-x64": {},
"win8-x64": {},
"win7-x64": {}
}
这在Windows机器上是有意义的,而不是在亚马逊上。我需要一些不同的runtime(s)
吗?
这一变化并未改变JsonReaderException
例外。
我尝试添加"Linux": {}
,但也没有改变。
我甚至试图将Microsoft.NETCore.App
的NuGet包1.0.0
更新为1.1.0
,并且没有做任何事情,甚至回到1.0.1
。
我想他们给你的默认例子可以工作,但我错了。似乎Amazon.Lambda.Serialization.Json.JsonSerializer
属性存在问题。是否可以只使用NewtonSoft
?
我在C#中使用AWS Lambda的经验有点受限,但是当输入(如果从控制台触发该功能的“测试”数据)不是实际的JSON格式时,我得到的这种错误。最好的选择可能是为您的测试目的创建一个虚拟输入类,然后为lambda函数提供等效的JSON数据,以使其正确序列化。
至于你的project.json,我之前不需要为AWS Lambda添加运行时部分。如果您这样做,则需要从此列表中选择相关的运行时:https://docs.microsoft.com/en-us/dotnet/articles/core/rid-catalog
如果你发现有用的话,这里还有一个示例project.json文件:https://gist.github.com/TAGray/8cc812065c3b6abcd732b7f6a3bda92a
希望有所帮助。
Per Pavel Safronov -
是的,如果您的Lambda函数入口点是:您应该输入字符串而不是json对象:
public string KevinsTestFunction(string input, ILambdaContext context) {
然后你可以输入的字符串是:
"{ \"input\": \"Something\" }"
您还可以将Lambda函数入口点更改为:
public string KevinsTestFunction(JObject input, ILambdaContext context) {
然后你可以这样输入一个json对象:
{ "input": "Something" }
如果您不期望任何输入并仅轮询SQS队列或DynamoDb表,那么您可以将Lambda函数的入口点更改为:
public string KevinsTestFunction(ILambdaContext context) {
可以使用许多变体。
现在,根据原始问题的更新,默认情况下,AWS Web控制台TEST区域将为您设置默认测试数据以发送到Lambda函数。该默认测试数据是JSON对象。如上所述,它与通过AWS SDK通过Visual Studio提供的C#Lambdas(接受字符串输入)的默认模板不一致。也许这是一个内置的绊脚石(特征),迫使我们遇到这个问题,把头发拉过来,然后意识到函数处理程序的多样性......
更新代码:
public string KevinsTestFunction(string input, ILambdaContext context) {
LambdaLogger.Log("Within the KTF");
return input?.ToUpper();
}
至:
public string KevinsTestFunction(IDictionary input, ILambdaContext context) {
LambdaLogger.Log("Within the KTF");
return input["input"]?.ToUpper();
}
不要忘记
using System.Collections;