个人而言,我还没有在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 值,并按照以下示例代码使用:
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字符串或对象来传递需要的多个变量。