【发布时间】:2017-12-30 22:08:28
【问题描述】:
我有一个 Lambda 函数,它需要 3 个参数
public async Task<string> FunctionHandler(string pName, string dictName, ILambdaContext context)
{
//code...
}
【问题讨论】:
标签: c# amazon-web-services lambda aws-lambda
我有一个 Lambda 函数,它需要 3 个参数
public async Task<string> FunctionHandler(string pName, string dictName, ILambdaContext context)
{
//code...
}
【问题讨论】:
标签: c# amazon-web-services lambda aws-lambda
就我个人而言,我没有在 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 字符串或对象来传递一个需要的多个变量,那可能是最好的。
【讨论】: