我正在尝试使用/设置 AWS .NET Core 3.1 Mock Lambda 测试工具。
目前我打算通过该工具启动应用程序,但是,一旦我尝试发送请求,我就会收到错误“无法找到方法 Init”。
在 aws-lambda-tools-defaults.json
中,我将 function-handler
设置为以下内容:
"function-handler": "Some.Example.Assembly::Some.Example.Namespace.LambdaProgram::Init"
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>();
}
}
}
格式对我来说似乎是正确的,除非我误读了文档?
在 bin/
目录中,dll 和 exe 具有匹配的名称,即“Some.Example.Assembly.exe”和“Some.Example.Assembly.dll”。
如果我更改function-handler
路径,那么我可以让它针对该类型抛出错误。但我不明白为什么找不到函数Init?应用程序构建并且 LambdaProgram
正在根据需要实现 AWS 接口(interface)。
任何帮助都会很棒,我真的希望能够在部署之前在本地进行测试/调试(这是生产中的现有应用程序 - 这只是 lamabda 迁移的一个例子)
最佳答案
在完全困惑了几个小时之后,我找到了解决方案。这可能在文档中,但我没有看到它,但 function-handler
不是 LambdaProgram 类(或您所称的任何类)中的函数。
相反,您应该使用 FunctionHandlerAsync
,我猜它是被继承的。
我通过克隆 official repository 找到了这个并查看他们的样本,其中有一条评论对此进行了详细说明!
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>();
}
}
}
关于c# - 找不到方法 - AWS .NET Core 3.1 Mock Lambda 测试工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61463675/