找不到方法 - AWS .NET Core 3.1 Mock Lambda 测试工具

rhy*_*bbs 3 c# amazon-web-services aws-lambda .net-core-3.1

我正在尝试使用/设置 AWS .NET Core 3.1 Mock Lambda 测试工具。

目前,我将通过该工具启动应用程序,但是,一旦我尝试发送请求,我就会收到错误“无法找到方法 Init”。

aws-lambda-tools-defaults.jsonI 中,将设置function-handler为以下内容:

"function-handler": "Some.Example.Assembly::Some.Example.Namespace.LambdaProgram::Init"
Run Code Online (Sandbox Code Playgroud)

LambdaProgram.cs 文件如下所示:

using Amazon.Lambda.AspNetCoreServer;
using Microsoft.AspNetCore.Hosting;

namespace Some.Example.Namespace
{
    public class LambdaProgram : APIGatewayHttpApiV2ProxyFunction
    {
        protected override void Init(IWebHostBuilder builder)
        {
            builder.UseStartup<Startup>();
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

除非我误读了文档,否则格式对我来说似乎是正确的?

bin/目录中,dll 和 exe 具有匹配的名称,即“Some.Example.Assembly.exe”和“Some.Example.Assembly.dll”。

如果我更改function-handler路径,那么我可以让它抛出该类型的错误。但是我不明白为什么它找不到Init函数?应用程序构建并根据LambdaProgram需要实施 AWS 接口。

任何帮助都会很棒,我真的希望能够在部署之前在本地进行测试/调试(这是生产中的现有应用程序 - 这只是 lamabda 迁移的一个案例)

rhy*_*bbs 5

在完全困惑了几个小时之后,我找到了解决方案。这可能在文档中,我没有看到它,但function-handler它不是 LambdaProgram 类(或任何您调用的类)中的函数。

相反,你应该使用FunctionHandlerAsync我猜是被继承的。

我通过克隆官方存储库并查看他们的示例发现了这一点,其中有一条评论详细说明了这一点!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace BlueprintBaseName._1
{
    /// <summary>
    /// This class extends from APIGatewayProxyFunction which contains the method FunctionHandlerAsync which is the 
    /// actual Lambda function entry point. The Lambda handler field should be set to
    /// 
    /// BlueprintBaseName.1::BlueprintBaseName.1.LambdaEntryPoint::FunctionHandlerAsync
    /// </summary>
    public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction

        protected override void Init(IWebHostBuilder builder)
        {
            builder
                .UseStartup<Startup>();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)