Lambda模拟测试工具.NET8.0原生AOT

问题描述 投票:0回答:1

我使用 dotnet new serverless.NativeAOT 启动了一个项目,因此我的项目使用注释框架并编译为 Native AOT。我可以将函数部署到 aws lambda,但它会引发空引用异常。我一直在尝试设置模拟 lambda 测试工具,以便我可以设置一些断点并进行诊断,但我所有的尝试都失败了。我使用的是 Visual Studio 2022,可以启动模拟测试工具,但“可执行程序集”选项卡中排队的事件只是挂在那里,不会命中我在 Startup.cs 或 Functions.cs 中设置的断点。

launchSettings.json

{
    "profiles": {
      "Lambda Runtime API": {
        "commandName": "Executable",
        "commandLineArgs": "--port 5050",
        "executablePath": "/Users/xxx/.dotnet/tools/dotnet-lambda-test-tool-8.0.exe",
        "workingDirectory": "/Users/xxx/source/repos/SendTunesServerless/SendTunesServerless/src/SendTunesServerless/bin/Debug/net8.0",
        "environmentVariables": {
          "AWS_LAMBDA_RUNTIME_API": "localhost:5050",
          "AWS_PROFILE": "default",
          "AWS_REGION": "us-east-1"
        }
      }
    }
  }

启动.cs

 [LambdaStartup]
 public class Startup
 {
    
     public void ConfigureServices(IServiceCollection services)
     {
         services.AddSingleton<ISpotifyAccessTokenService, SpotifyAccessTokenService>();

         services.AddHttpClient<ISpotifyService, SpotifyService>(client =>
         {
             client.BaseAddress = new Uri("https://api.spotify.com/v1/");
         });
     }
 }

有人能够使用 lambda 模拟测试工具成功测试 .NET8.0 Native AOT 吗?

我已经完成了 https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool?ref=rahulpnath.com#configure-for-visual-studio 和测试可执行程序集工具中的参考。我预计会遇到断点或引发错误,但队列中的事件让我认为我的设置不正确。

.net amazon-web-services aws-lambda native-aot
1个回答
0
投票

我能够复制您描述的问题,并且找到了解决方案。以下是我采取的步骤:

  1. 从 Visual Studio 创建了一个具有相同模板(与您使用的)的新项目。

  2. 之后,只需按照我在步骤 7. 在 Visual Studio 中调试和测试 Lambda 函数

    中提到的步骤操作即可
  3. 使用新环境变量更新了

    launchSettings.json
    文件,如下所述。

在 .NET Lambda 注释框架中调试可执行程序集 Lambda 函数

基于类库的 Lambda 函数的处理程序名称遵循语法

ASSEMBLY::TYPE::METHOD
。这指定了将执行的程序集、类和方法。

对于基于可执行程序集的 Lambda 函数,处理程序名称只是程序集名称。

对于可执行程序集,需要在环境变量中指定方法名称。然后使用此环境变量将适当的委托方法传递给自动生成的

Main
方法。

解决方案

作为解决方案,您需要在

ANNOTATIONS_HANDLER
文件中再添加一个环境变量
launchSettings.json

{
  "profiles": {
    "AWSServerlessNativeAOT.Example": {
      "commandName": "Project",
      "environmentVariables": {
        "AWS_LAMBDA_RUNTIME_API": "localhost:5050",
        "ANNOTATIONS_HANDLER": "GetFunctionHandler"
      }
    }
  }
}

因为,如果你看到自动生成的

Program.cs
文件,你会发现它需要这个环境变量来决定执行哪个Function。

© www.soinside.com 2019 - 2024. All rights reserved.