就我個人而言,我沒有在Lambda入口點嘗試使用異步任務,因此無法對此進行評論。
然而,另一種方式去了解它是lambda函數入口點更改爲:
public async Task<string> FunctionHandler(JObject input, ILambdaContext context)
然後拉兩個變量出來,像這樣:
string dictName = input["dictName"].ToString();
string pName = input["pName"].ToString();
然後在您輸入的AWS Web控制檯:
{
"dictName":"hello",
"pName":"kitty"
}
或者,您也可以採用JObject值並使用i t,如以下示例代碼所示:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace SimpleJsonTest
{
[TestClass]
public class JsonObjectTests
{
[TestMethod]
public void ForgiveThisRunOnJsonTestJustShakeYourHeadSayUgghhhAndMoveOn()
{
//Need better names than dictName and pName. Kept it as it is a good approximation of software potty talk.
string json = "{\"dictName\":\"hello\",\"pName\":\"kitty\"}";
JObject jsonObject = JObject.Parse(json);
//Example Zero
string dictName = jsonObject["dictName"].ToString();
string pName = jsonObject["pName"].ToString();
Assert.AreEqual("hello", dictName);
Assert.AreEqual("kitty", pName);
//Example One
MeaningfulName exampleOne = jsonObject.ToObject<MeaningfulName>();
Assert.AreEqual("hello", exampleOne.DictName);
Assert.AreEqual("kitty", exampleOne.PName);
//Example Two (or could just pass in json from above)
MeaningfulName exampleTwo = JsonConvert.DeserializeObject<MeaningfulName>(jsonObject.ToString());
Assert.AreEqual("hello", exampleTwo.DictName);
Assert.AreEqual("kitty", exampleTwo.PName);
}
}
public class MeaningfulName
{
public string PName { get; set; }
[JsonProperty("dictName")] //Change this to suit your needs, or leave it off
public string DictName { get; set; }
}
}
問題是我不知道在AWS Lambda中是否可以有兩個輸入變量。賠率是你不能。除此之外,如果您堅持使用json字符串或對象來傳遞所需的多個變量,那麼這可能是最好的選擇。