【问题标题】:aws lambda function using serverless template of asp.net core使用 asp.net 核心的无服务器模板的 aws lambda 函数
【发布时间】:2021-03-15 11:43:39
【问题描述】:

我没有足够的 AWS 知识,但我的公司要求我做一份工作,我猜这就是 AWS Lambda 可以完美完成的工作。要求是我必须创建一个服务,该服务的端点需要每天调用两次。我采用的方法是通过 Visual Studio 创建了一个无服务器 Web API,并为每个端点创建了 API 网关端点。然后通过云观察事件添加了一个触发器,每天运行两次,但每当触发该函数时,我都会收到此错误。

Object reference not set to an instance of an object.: NullReferenceException
   at Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction.MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
   at Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction`2.FunctionHandlerAsync(TREQUEST request, ILambdaContext lambdaContext)
   at lambda_method(Closure , Stream , Stream , LambdaContextInternal )

【问题讨论】:

  • 如果您使用 cloudwatch 事件调用用 .NET 编写的 Lambda 函数,为什么要使用 API Gateway?您可以直接从 cloudwatch 事件调用 Lambda 函数。

标签: amazon-web-services lambda triggers asp.net-core-webapi aws-serverless


【解决方案1】:

我有同样的问题,最近可以解决。

如果您将 Lambda 与 ASP.NET Core 一起使用,您应该有 LambdaEntryPoint 类来处理所有请求。 尝试在此类中覆盖MarshallRequest 方法,添加日志记录并查看apiGatewayRequest 参数中的内容。代码可能如下所示:

protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
{
    LambdaLogger.Log($"Request path: {apiGatewayRequest.Path}");
    LambdaLogger.Log($"Request path parameters: {apiGatewayRequest.PathParameters}");
    LambdaLogger.Log($"Request body: {apiGatewayRequest.Body}");
    LambdaLogger.Log($"Request request context: {apiGatewayRequest.RequestContext}");
    base.MarshallRequest(features, apiGatewayRequest, lambdaContext);
}

就我而言,所有这些值都是空值。其原因是使用 Amazon EventBridge 保持 Lambda 在线以避免冷启动。如果您还使用 EventBridge,请尝试在那里正确配置请求。如果没有,您可以尝试通过以下方式更新MarshalRequest

protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
{
    if(apiGatewayRequest.RequestContext == null) //Or other property
    {
        return;
    }

    base.MarshallRequest(features, apiGatewayRequest, lambdaContext);
}

【讨论】:

    【解决方案2】:

    几天前我遇到了同样的问题。 Grigory Zhadko 的回答对我有很大帮助,因为我知道我应该覆盖哪种方法。 LambdaEntryPoint 要求任何其他进程手动实例化 ApiGatewayProxiRequest 对象(例如,eventBridge)。我为解决问题而实施的配置如下。

    protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
        {
            var endpoint = "my/endpoint";
    
            if (apiGatewayRequest != null && apiGatewayRequest?.RequestContext == null)
            {
                apiGatewayRequest.Path = $"/{endpoint}";
                apiGatewayRequest.Resource = $"/{endpoint}";
                apiGatewayRequest.HttpMethod = "ANY METHOD";
                apiGatewayRequest.RequestContext = new APIGatewayProxyRequest.ProxyRequestContext
                {
                    Path = $"/path/{endpoint}", // your path request
                    Identity = new APIGatewayProxyRequest.RequestIdentity
                    {
                        ClientCert = new APIGatewayProxyRequest.ProxyRequestClientCert
                        {
                            Validity = new APIGatewayProxyRequest.ClientCertValidity()
                        }
                    },
                    ResourcePath = $"/{basePath}{eventEntpoint}",
                    HttpMethod = "ANY METHOD",
                    Authorizer = new APIGatewayCustomAuthorizerContext()
                };
            }
    
            base.MarshallRequest(features, apiGatewayRequest, lambdaContext);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2017-01-29
      • 2019-07-23
      • 2017-10-01
      • 1970-01-01
      • 2018-01-21
      相关资源
      最近更新 更多