首页 > 解决方案 > 在 VS2019 中使用 API Gateway 部署到 AWS Lambda 会给出 {"message":"Internal Server Error"}

问题描述

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

标签: c#amazon-web-servicesaws-lambdaaws-api-gateway

解决方案


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

我发现这个答案很有用

此代码有效:

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

推荐阅读