如果还需要对象类型参数,如何在字符串类型函数中传递参数?
How to pass a parameter in function of type string if it also requires an object type param?
我有函数处理程序
namespace AWSLambda2
{
public class input
{
public string name { get; set; }
public string email { get; set; }
}
public class Function
{
public string FunctionHandler(string data, input input)
{
return input.name?.ToUpper()+", "+input.email?.ToUpper()+", "+data;
}
}
}
它接受一个字符串类型的参数和对象类型的输入。很好
我可以传递的对象类型:
{
"name":"test",
"email": "test@gmail.com"
}
但它会抛出错误,因为我也必须传递数据参数。如何在使用 POSTMAN 或 Swagger 为 POST 请求传递 JSON 负载时传递它。
为什么你不能有一个 DTO 来保存下面的数据并让你的处理程序接受该类型 public string FunctionHandler(MyModel model)
public class MyModel
{
public string Data {get; set;}
public Input Input {get; set;}
}
我明白了,然后通过查询字符串传递额外的数据并定义您的 Api 处理程序,例如
public string FunctionHandler([FromQuery]string data, [FromBody]input input)
根据AWS Lambda Function Handler in C#
You define a Lambda function handler as an instance or static method
in a class. If you want access to the Lambda context object, it is
available by defining a method parameter of type ILambdaContext, an
interface you can use to access information about the current
execution, such as the name of the current function, the memory limit,
execution time remaining, and logging.
returnType handler-name(inputType input, ILambdaContext context) {
...
}
In the syntax, note the following:
inputType
– The first handler parameter is the input to the handler,
which can be event data (published by an event source) or custom input
that you provide such as a string or any custom data object.
returnType
– If you plan to invoke the Lambda function synchronously
(using the RequestResponse invocation type), you can return the output
of your function using any of the supported data types. For example,
if you use a Lambda function as a mobile application backend, you are
invoking it synchronously. Your output data type will be serialized
into JSON.
If you plan to invoke the Lambda function asynchronously (using the
Event invocation type), the returnType should be void. For example, if
you use AWS Lambda with event sources such as Amazon S3 or Amazon SNS,
these event sources invoke the Lambda function using the Event
invocation type.
根据 Working with Lambda Functions : Programming Model
You write code for your Lambda function in one of the languages AWS Lambda supports. Regardless of the language you choose, there is a common pattern to writing code for a Lambda function that includes the following core concepts:
- Handler – Handler is the function AWS Lambda calls to start execution of your Lambda function. You identify the handler when you create your Lambda function. When a Lambda function is invoked, AWS Lambda starts executing your code by calling the handler function. AWS Lambda passes any event data to this handler as the first parameter. Your handler should process the incoming event data and may invoke any other functions/methods in your code.
- The context object and how it interacts with Lambda at runtime – AWS Lambda also passes a context object to the handler function, as the second parameter. Via this context object your code can interact with AWS Lambda. For example, your code can find the execution time remaining before AWS Lambda terminates your Lambda function.
In addition, for languages such as Node.js, there is an asynchronous platform that uses callbacks. AWS Lambda provides additional methods on this context object. You use these context object methods to tell AWS Lambda to terminate your Lambda function and optionally return values to the caller.
.....
强调我的。
所以回答你的标题
How to pass a parameter in function of type string if it also requires an object type param?
简答:你不能
我建议创建一个特定的模型来保存所需的数据
public class SampleClass {
public string data { get; set; }
public Input input { get; set; }
}
并将其用于函数输入
namespace AWSLambda2 {
public class Input {
public string name { get; set; }
public string email { get; set; }
}
public class Function {
public string FunctionHandler(SampleClass input) {
var data = input.data;
var user = input.input;
return user.name?.ToUpper()+", "+user.email?.ToUpper()+", "+data;
}
}
}
与所提供示例中的显示方式类似
https://github.com/guitarrapc/AWSLambdaCSharpIntroduction/tree/master/src
我有函数处理程序
namespace AWSLambda2
{
public class input
{
public string name { get; set; }
public string email { get; set; }
}
public class Function
{
public string FunctionHandler(string data, input input)
{
return input.name?.ToUpper()+", "+input.email?.ToUpper()+", "+data;
}
}
}
它接受一个字符串类型的参数和对象类型的输入。很好
我可以传递的对象类型:
{
"name":"test",
"email": "test@gmail.com"
}
但它会抛出错误,因为我也必须传递数据参数。如何在使用 POSTMAN 或 Swagger 为 POST 请求传递 JSON 负载时传递它。
为什么你不能有一个 DTO 来保存下面的数据并让你的处理程序接受该类型 public string FunctionHandler(MyModel model)
public class MyModel
{
public string Data {get; set;}
public Input Input {get; set;}
}
我明白了,然后通过查询字符串传递额外的数据并定义您的 Api 处理程序,例如
public string FunctionHandler([FromQuery]string data, [FromBody]input input)
根据AWS Lambda Function Handler in C#
You define a Lambda function handler as an instance or static method in a class. If you want access to the Lambda context object, it is available by defining a method parameter of type ILambdaContext, an interface you can use to access information about the current execution, such as the name of the current function, the memory limit, execution time remaining, and logging.
returnType handler-name(inputType input, ILambdaContext context) { ... }
In the syntax, note the following:
inputType
– The first handler parameter is the input to the handler, which can be event data (published by an event source) or custom input that you provide such as a string or any custom data object.
returnType
– If you plan to invoke the Lambda function synchronously (using the RequestResponse invocation type), you can return the output of your function using any of the supported data types. For example, if you use a Lambda function as a mobile application backend, you are invoking it synchronously. Your output data type will be serialized into JSON.
If you plan to invoke the Lambda function asynchronously (using the Event invocation type), the returnType should be void. For example, if you use AWS Lambda with event sources such as Amazon S3 or Amazon SNS, these event sources invoke the Lambda function using the Event invocation type.
根据 Working with Lambda Functions : Programming Model
You write code for your Lambda function in one of the languages AWS Lambda supports. Regardless of the language you choose, there is a common pattern to writing code for a Lambda function that includes the following core concepts:
- Handler – Handler is the function AWS Lambda calls to start execution of your Lambda function. You identify the handler when you create your Lambda function. When a Lambda function is invoked, AWS Lambda starts executing your code by calling the handler function. AWS Lambda passes any event data to this handler as the first parameter. Your handler should process the incoming event data and may invoke any other functions/methods in your code.
- The context object and how it interacts with Lambda at runtime – AWS Lambda also passes a context object to the handler function, as the second parameter. Via this context object your code can interact with AWS Lambda. For example, your code can find the execution time remaining before AWS Lambda terminates your Lambda function.
In addition, for languages such as Node.js, there is an asynchronous platform that uses callbacks. AWS Lambda provides additional methods on this context object. You use these context object methods to tell AWS Lambda to terminate your Lambda function and optionally return values to the caller......
强调我的。
所以回答你的标题
How to pass a parameter in function of type string if it also requires an object type param?
简答:你不能
我建议创建一个特定的模型来保存所需的数据
public class SampleClass {
public string data { get; set; }
public Input input { get; set; }
}
并将其用于函数输入
namespace AWSLambda2 {
public class Input {
public string name { get; set; }
public string email { get; set; }
}
public class Function {
public string FunctionHandler(SampleClass input) {
var data = input.data;
var user = input.input;
return user.name?.ToUpper()+", "+user.email?.ToUpper()+", "+data;
}
}
}
与所提供示例中的显示方式类似
https://github.com/guitarrapc/AWSLambdaCSharpIntroduction/tree/master/src