簡體   English   中英

aws lambda function 使用 asp.net 核心的無服務器模板

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

我對 aws 沒有足夠的了解,但我的公司要求我做一份工作,我猜這就是 AWS Lambda 完美的工作。 要求是我必須創建一個具有需要每天調用兩次的端點的服務。 我遵循的方法是通過 Visual Studio 創建了一個無服務器 web API 並為每個端點創建了 API 網關端點。 然后通過雲監視事件添加一個觸發器以每天運行兩次,但是每當觸發 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 )

我有同樣的問題,最近可以解決。

如果您將 Lambda 與 ASP.NET 內核一起使用,您應該有LambdaEntryPoint class 來處理所有請求。 嘗試在此 class 中覆蓋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);
}

幾天前我遇到了同樣的問題,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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM