我正在嘗試在C#中編寫AWS Lambda函數。我擁有適用於Visual Studio 2015的AWS工具包。我使用AWS Lambda項目(.Net Core)創建了一個項目,然後選擇了空函數選項。這給了我下面的代碼:C#中的Amazon Lambda - JsonReaderException
UPDATE & ANSWER 17年2月24日 - 註釋標記爲答案是有用的知識,但不是我實際的答案。這是@PavelSafronov的評論,在那個答案,做了伎倆。我要麼通過什麼都沒有(並得到錯誤),或者我認爲它要我以JSON格式提供信息,所以我會輸入{ "input": "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": {}
但是那也沒有改變。
我甚至試圖從1.0.0
更新NuGet包Microsoft.NETCore.App
到1.1.0
並沒有採取任何行動,以及甚至回1.0.1
。
我想他們給你的默認例子會工作,但我錯了。看起來好像Amazon.Lambda.Serialization.Json.JsonSerializer
屬性存在問題。是否可以使用NewtonSoft
?
回覆:「但是,儘可能我擔心,這看起來像一個bug。一個字符串默認爲空值...「 - 不帶引號的空值是有效的JSON,如果它傳遞給一個需要字符串的函數,字符串參數將爲空。這可以通過使用AWS Lambda控制檯並指定'null'作爲測試JSON有效負載來驗證,而不是* AWS *工具包,因爲Toolkit會將未加引號的文本視爲字符串(爲了方便起見)並在將其傳遞到Lambda之前引用它。 –