C# 将参数传递给AWS Lambda函数
我有一个Lambda函数,它假设有3个参数C# 将参数传递给AWS Lambda函数,c#,amazon-web-services,lambda,aws-lambda,C#,Amazon Web Services,Lambda,Aws Lambda,我有一个Lambda函数,它假设有3个参数 public async Task<string> FunctionHandler(string pName, string dictName, ILambdaContext context) { //code... } public async Task FunctionHandler(字符串pName、字符串dictName、ILambdaContext上下文) { //代码。。。 } 我使用的是Visual Studio 2015
public async Task<string> FunctionHandler(string pName, string dictName, ILambdaContext context)
{
//code...
}
public async Task FunctionHandler(字符串pName、字符串dictName、ILambdaContext上下文)
{
//代码。。。
}
我使用的是Visual Studio 2015,我将此发布到AWS环境,我应该在示例输入框中输入什么来调用此函数?
就我个人而言,我没有在Lambda入口点中尝试过异步任务,因此无法对此发表评论 但是,另一种方法是将Lambda函数入口点更改为:
public async Task<string> FunctionHandler(JObject input, ILambdaContext context)
然后在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; }
}
}
使用系统;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用Newtonsoft.Json.Linq;
使用Newtonsoft.Json;
名称空间SimpleJsonTest
{
[测试类]
公共类JsonObjectTests
{
[测试方法]
公共无效豁免违法测试只需摇摇你的头说UgghhandMoveon()
{
//需要比dictName和pName更好的名称。保留它,因为它是一个很好的近似软件便盆谈话。
字符串json=“{\”dictName\”:\“hello\”,\“pName\”:\“kitty\”}”;
JObject jsonObject=JObject.Parse(json);
//示例零
字符串dictName=jsonObject[“dictName”].ToString();
字符串pName=jsonObject[“pName”].ToString();
Assert.AreEqual(“hello”,dictName);
断言.AreEqual(“kitty”,pName);
//例一
MeaningfulName exampleOne=jsonObject.ToObject();
arenequal(“hello”,exampleOne.DictName);
AreEqual(“kitty”,exampleOne.PName);
//示例二(或者可以从上面传入json)
MeaningfulName exampleTwo=JsonConvert.DeserializeObject(jsonObject.ToString());
arenequal(“hello”,example2.DictName);
Assert.AreEqual(“kitty”,example2.PName);
}
}
公共类有意义的名称
{
公共字符串PName{get;set;}
[JsonProperty(“dictName”)]//更改此选项以满足您的需要,或者不使用它
公共字符串DictName{get;set;}
}
}
问题是我不知道在AWS Lambda中是否可以有两个输入变量。很可能你不能。此外,最好还是使用json字符串或对象来传递所需的多个变量
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; }
}
}