【问题标题】:Deploying to AWS Lambda in VS2019 with API Gateway gives {"message":"Internal Server Error"}在 VS2019 中使用 API Gateway 部署到 AWS Lambda 会给出 {"message":"Internal Server Error"}
【发布时间】:2020-12-20 01:49:15
【问题描述】:

通过 Lambda 控制台测试 Lambda 本身;我传入“asdf”并得到“ASDF”作为响应。

但是,当我添加 API 网关时:

我收到 {"message":"Internal Server Error"}

此代码有效(删除input 参数):

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 "Hello, World";
        }
    }
}

此代码不起作用:

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();
        }
    }
}

【问题讨论】:

  • 这是因为您需要添加 API Gateway 支持。首先,添加Amazon.Lambda.APIGatewayEvents。然后,更改您的函数签名以接收APIGatewayProxyRequest request, ILambdaContext context 并返回APIGatewayProxyResponse

标签: c# amazon-web-services aws-lambda aws-api-gateway


【解决方案1】:

正如@zaitsman 指出的,我需要使用Amazon.Lambda.APIGatewayEvents NuGet 包。

我找到了this answer useful

此代码有效:

        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!"
            };
        }

【讨论】:

    猜你喜欢
    • 2022-09-23
    • 2021-08-08
    • 2020-05-19
    • 2021-09-11
    • 2017-06-12
    • 2015-10-05
    • 2020-04-18
    • 2021-07-05
    • 2020-03-17
    相关资源
    最近更新 更多