C# 使用API网关部署到VS2019中的AWS Lambda会给出{“消息”:“内部服务器错误”}
从Lambda控制台测试Lambda本身;我传入“asdf”并得到“asdf”作为响应 但是,当我添加API网关时:C# 使用API网关部署到VS2019中的AWS Lambda会给出{“消息”:“内部服务器错误”},c#,amazon-web-services,aws-lambda,aws-api-gateway,C#,Amazon Web Services,Aws Lambda,Aws Api Gateway,从Lambda控制台测试Lambda本身;我传入“asdf”并得到“asdf”作为响应 但是,当我添加API网关时: 转到Lambda控制台: 添加触发器 API网关 HTTP API(REST API也有同样的问题) 打开 启用CORS 我收到一条{“消息”:“内部服务器错误”} 此代码有效(删除输入参数): 此代码不起作用: using System; using System.Collections.Generic; using System.Linq; using System.Th
- 转到Lambda控制台:
- 添加触发器
- API网关
- HTTP API(REST API也有同样的问题)
- 打开
- 启用CORS
输入
参数):
此代码不起作用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Complyify.Contexts;
using Complyify.Engines;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace ComplyifyLambda
{
public class Function
{
public string FunctionHandler(string input, ILambdaContext context)
{
return input?.ToUpper();
}
}
}
正如@zaitsman指出的,我需要使用
Amazon.Lambda.APIGatewayEvents
NuGet包
我找到了
这一准则起了作用:
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
var bodyString = request?.Body;
if (!string.IsNullOrEmpty(bodyString))
{
return new APIGatewayProxyResponse
{
StatusCode = 200,
Body = bodyString.ToUpper()
};
}
return new APIGatewayProxyResponse
{
StatusCode = 200,
Body = "No body!"
};
}
这是因为您需要添加API网关支持。首先,添加
Amazon.Lambda.APIGatewayEvents
。然后,将函数签名更改为接收APIGatewayProxyRequest请求、ILambdaContext上下文
,并返回APIGatewayProxyResponse
。
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
var bodyString = request?.Body;
if (!string.IsNullOrEmpty(bodyString))
{
return new APIGatewayProxyResponse
{
StatusCode = 200,
Body = bodyString.ToUpper()
};
}
return new APIGatewayProxyResponse
{
StatusCode = 200,
Body = "No body!"
};
}