Linux下VS Code调试.NET 7.0 NativeAOT Lambda函数遇阻求助
.NET 7 NativeAOT Lambda 本地测试解决方案
核心结论
不需要Docker、SAM,也不用Executable Assembly页面,仅通过Mock Lambda Test Tool 7.0 + 正确配置就能完成测试。问题出在当前launch.json配置错误,以及未正确构建NativeAOT输出文件。
修复步骤
1. 先正确构建NativeAOT Lambda项目
NativeAOT需要发布(publish)才能生成可执行的bootstrap文件,执行以下命令:
dotnet publish -c Release -r linux-x64
执行后,可执行文件会生成在${workspaceFolder}/DocGenerator/bin/Release/net7.0/linux-x64/publish目录下。
2. 修改launch.json配置
当前配置的program指向测试工具本身,但缺少指定Lambda可执行文件的参数,且preLaunchTask应该是发布而非构建。修改后的配置如下:
{ "version": "0.2.0", "configurations": [ { "name": "Mock Lambda Test Tool (NativeAOT)", "type": "coreclr", "request": "launch", "preLaunchTask": "publish", // 改为publish,确保每次启动前生成最新bootstrap "program": "${env:HOME}/.dotnet/tools/dotnet-lambda-test-tool-7.0", "args": [ "--function-handler", "bootstrap", "--function-runtime", "provided.al2", "--function-directory", "${workspaceFolder}/DocGenerator/bin/Release/net7.0/linux-x64/publish", "--port", "5050" ], "cwd": "${workspaceFolder}/DocGenerator", "console": "internalConsole", "stopAtEntry": false }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach" } ] }
3. 添加publish任务到tasks.json(如果没有的话)
在.vscode目录下创建tasks.json,添加发布任务,确保启动测试工具前自动完成发布:
{ "version": "2.0.0", "tasks": [ { "label": "publish", "command": "dotnet", "args": [ "publish", "-c", "Release", "-r", "linux-x64" ], "type": "process", "problemMatcher": "$msCompile", "group": { "kind": "build", "isDefault": true } } ] }
4. 启动测试工具并发送请求
- 点击VS Code调试面板的
Mock Lambda Test Tool (NativeAOT)启动测试工具 - 打开浏览器访问
http://localhost:5050 - 在测试页面的"Request"标签页,输入符合Lambda请求格式的JSON(比如针对字符串转大写的函数,请求体可以是:
{ "body": "hello world" }
- 点击"Invoke"按钮,就能在"Response"标签页看到转换后的大写结果。
额外说明
- NativeAOT Lambda的handler是
bootstrap(原生可执行文件),所以测试工具需要指定--function-handler bootstrap --function-runtime provided.al2对应你在aws-lambda-tools-default.json里配置的运行时,必须一致- 如果函数的输入不是
body字段,要根据你的函数代码调整请求JSON格式(比如如果是接收APIGateway请求,要符合APIGateway的事件结构)
内容的提问来源于stack exchange,提问作者TrinaE
相关产品推荐
相关产品推荐

