Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用API网关部署到VS2019中的AWS Lambda会给出{“消息”:“内部服务器错误”}_C#_Amazon Web Services_Aws Lambda_Aws Api Gateway - Fatal编程技术网

C# 使用API网关部署到VS2019中的AWS Lambda会给出{“消息”:“内部服务器错误”}

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控制台测试Lambda本身;我传入“asdf”并得到“asdf”作为响应

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

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