简体   繁体   English

aws lambda function 使用 asp.net 核心的无服务器模板

[英]aws lambda function using serverless template of asp.net core

I don't have enough knowledge of aws but my company asked me to do a job which I guess is what AWS Lambda does perfectly.我对 aws 没有足够的了解,但我的公司要求我做一份工作,我猜这就是 AWS Lambda 完美的工作。 The requirement is I have to create a service that has an endpoint that needs to be called twice a day.要求是我必须创建一个具有需要每天调用两次的端点的服务。 The approach I followed is I created a serverless web API through visual studio and created API gateway endpoint for each endpoint.我遵循的方法是通过 Visual Studio 创建了一个无服务器 web API 并为每个端点创建了 API 网关端点。 Then added a trigger through cloud watch events to run it twice a day but whenever the function is triggered I get this error.然后通过云监视事件添加一个触发器以每天运行两次,但是每当触发 function 时,我都会收到此错误。

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 )

I have the same issue and could fix it recently.我有同样的问题,最近可以解决。

If you use Lambda with ASP.NET Core, you should have LambdaEntryPoint class to handle all the requests.如果您将 Lambda 与 ASP.NET 内核一起使用,您应该有LambdaEntryPoint class 来处理所有请求。 Try to override MarshallRequest method in this class, add logging and see what you have in apiGatewayRequest parameter.尝试在此 class 中覆盖MarshallRequest方法,添加日志记录并查看apiGatewayRequest参数中的内容。 The code can look something like this:代码看起来像这样:

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

In my case, all these values were nulls.就我而言,所有这些值都是空值。 The reason for it was in using Amazon EventBridge for keeping Lambda online to avoid a cold start.其原因是使用 Amazon EventBridge 保持 Lambda 在线以避免冷启动。 If you also use EventBridge, try to configure the request there properly.如果您还使用 EventBridge,请尝试在那里正确配置请求。 If not, you can try to update MarshalRequest the following way:如果没有,您可以尝试通过以下方式更新MarshalRequest

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

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

A few days ago I had the same problem, Grigory Zhadko helped me a lot by knowing which method I should overwrite, LambdaEntryPoint requires for any other process to instantiate an ApiGatewayProxiRequest object manually (for example, eventBridge), the configuration I implemented to fix the problem is as follows.几天前我遇到了同样的问题,Grigory Zhadko 通过知道我应该覆盖哪个方法帮助了我很多,LambdaEntryPoint 需要任何其他进程手动实例化 ApiGatewayProxiRequest object(例如,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);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为ASP.NET Core lambda函数serverless.template文件设置AWS Authorizer条目 - Set AWS Authorizer entry for ASP.NET Core lambda function serverless.template file 如何使用 asp.net core 3.1 serverless 在 AWS lambda 中为复杂的配置参数设置环境变量? - How to set environment variables for complex configuration parameters in AWS lambda using asp.net core 3.1 serverless? 将自定义 AWS Lambda 运行时与 asp.net 核心结合使用 - Using Custom AWS Lambda Runtimes with asp.net core AWS Lambda-使用.NET Core构建无服务器API - AWS Lambda - Building serverless API using .NET Core ASP.NET AWS 无服务器 - ASP.NET AWS Serverless Error publishing ASP.NET Core Web API to AWS Serverless Lambda: 'AWSLambdaFullAccess' at 'policyArn' … Member must have length greater than - Error publishing ASP.NET Core Web API to AWS Serverless Lambda: 'AWSLambdaFullAccess' at 'policyArn' … Member must have length greater than ASP.NET AWS 中托管的 Core 6 应用程序 Lambda function URL 处理程序 - ASP.NET Core 6 app hosted in AWS Lambda function URL Handler 我们可以使用无服务器框架将 a.Net Core 3 服务部署到 AWS Lambda 吗? - Can we deploy a .Net Core 3 service to AWS Lambda using the Serverless Framework? 我们可以使用 AWS Lambda 编写 C# 控制台应用程序和 ASP.NET Core MVC Web 应用程序吗? - Can we write C# console application and ASP.NET Core MVC web application using AWS Lambda 使用无服务器将AWS sqs添加为AWS Lambda函数触发器 - Add aws sqs as aws lambda function trigger using serverless
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM