无法从程序集加载类型(C# Amazon lambda 函数)

Unable to load type from assembly (C# Amazon lambda function)

由于亚马逊现在支持C#构建AWS Lambda函数,我想尝试一下,但在执行测试时卡住了。

这是我的简单class:

using System;
using System.IO;
using System.Text;

using Amazon.Lambda.Core;
//using Amazon.Lambda.Serialization.Json;

namespace MyTest
{
    public static class LambdaFunction
    {
        public static string Handler(Stream stream)
        {
            return "Hello World";
        }
    }
}

我使用 .Net Core 运行时编译它。结果是包含程序集 MyTest.dll 文件和 MyTest.deps.json 文件的文件夹 netstandard1.4。这些压缩为 .zip 的文件被上传到 AWS Lambda 控制台。

在配置选项卡中,处理程序定义为:

MyTest::LambdaFunction::Handler

但是当我按下“测试”按钮时,返回的错误消息是这样的:

{
  "errorType": "LambdaException",
  "errorMessage": "Unable to load type 'LambdaFunction' from assembly 'MyTest, Culture=neutral, PublicKeyToken=null'."
}

注意 1:在我知道我需要使用 .Net Core 而不是完整的 CLR 之前,我收到一个无法加载程序集的错误,所以我认为程序集现在可以编译了。

注意 2:我已经为 Handler 方法以及 static/instance class 或方法或任何组合,都无济于事。

任何人谁已经得到这个工作,可以给我一些指示?

嗯,我想这是那些日子之一....

答案是,我忘记包含命名空间 8|

应该是:

MyTest::MyTest.LambdaFunction::Handler

今天早些时候遇到了同样的问题,我能够解决它,这是应该传递的正确格式:

AssemblyName::NameSpace.ClassName::FunctionHandlerName

偶然发现了这个问题,但设置程序集名称并没有为我解决它。

这是做了什么,是的......这是我代码中的一个错误。

LambdaEntryPoint.cs 立即传递给 Startup.cs。如果您的依赖项之一有问题或缺少 appsettings.{env}.json 文件,那么它会抛出异常。

这是另一种检查应用程序是否在生产中运行的方法。

$ export ASPNETCORE_ENVIRONMENT=Production
$ dotnet run

在我的例子中,缺少一个 appsettings.{env}.json 文件,该文件被设置为需要。不幸的是,lambda 给你一个非常匿名的错误,但错误可能在你的 Startup.cs 生产中。

这对大多数人没有帮助,但有 1% 的人忽略了一些简单的事情...

我也收到了这个错误信息。在我的例子中,我调用了我的函数 'HelloLambda' 但随后复制了一些开始的代码:

命名空间计算器。

将其更改为

命名空间 HelloLambda

解决了问题

我尝试了以上所有解决方案,但没有任何效果。

在我的案例中,解决方案是减少 AssemblyName::NameSpace.ClassName::FunctionHandlerName 组合的长度。此组合的长度超过 128 个字符,lambda 部署在 windows。

将长度减少到 少于 127 个字符 开始在配置文件中工作。

如果其他人遇到类似问题,请添加解决方案。

我的问题是体系结构不匹配。 Lambda 设置为 arm64,但生成的包是 x86_64,这是我在查看 dotnet lambda package 的输出时注意到的。 --runtime 设置为 linux-x64

使用 dotnet lambda package -farch arm64 解决了它。


dotnet lambda help package的输出:

Amazon Lambda Tools for .NET Core applications (5.2.0)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

package:
   Command to package a Lambda project either into a zip file or docker image if --package-type is set to "image". The output can later be deployed to Lambda with either deploy-function command or with another tool.

   dotnet lambda package [arguments] [options]
   Arguments:
      <ZIP-FILE> The name of the zip file to package the project into
   Options:
      --disable-interactive                   When set to true missing required parameters will not be prompted for.
      --region                                The region to connect to AWS services, if not set region will be detected from the environment.
      --profile                               Profile to use to look up AWS credentials, if not set environment credentials will be used.
      --profile-location                      Optional override to the search location for Profiles, points at a shared credentials file.
      --aws-access-key-id                     The AWS access key id. Used when setting credentials explicitly instead of using --profile.
      --aws-secret-key                        The AWS secret key. Used when setting credentials explicitly instead of using --profile.
      --aws-session-token                     The AWS session token. Used when setting credentials explicitly instead of using --profile.
      -pl    | --project-location             The location of the project, if not set the current directory will be assumed.
      -cfg   | --config-file                  Configuration file storing default values for command line arguments.
      -pcfg  | --persist-config-file          If true the arguments used for a successful deployment are persisted to a config file.
      -c     | --configuration                Configuration to build with, for example Release or Debug.
      -f     | --framework                    Target framework to compile, for example netcoreapp3.1.
      -farch | --function-architecture        The architecture of the Lambda function. Valid values: x86_64 or arm64. Default is x86_64
      --msbuild-parameters                    Additional msbuild parameters passed to the 'dotnet publish' command. Add quotes around the value if the value contains spaces.
      -fl    | --function-layers              Comma delimited list of Lambda layer version arns
      -pl    | --project-location             The location of the project, if not set the current directory will be assumed.
      -cfg   | --config-file                  Configuration file storing default values for command line arguments.
      -pcfg  | --persist-config-file          If true the arguments used for a successful deployment are persisted to a config file.
      -o     | --output-package               The zip file that will be created with compiled and packaged Lambda function.
      -dvc   | --disable-version-check        Disable the .NET Core version check. Only for advanced usage.
      -pt    | --package-type                 The deployment package type for Lambda function. Valid values: image, zip
      -it    | --image-tag                    Docker image name and tag in the 'name:tag' format.
      -df    | --dockerfile                   The docker file used to build the image. Default value is "Dockerfile".
      -dbo   | --docker-build-options         Additional options passed to the "docker build" command.
      -dbwd  | --docker-build-working-dir     The directory to execute the "docker build" command from.
      --docker-host-build-output-dir          If set a "dotnet publish" command is executed on the host machine before executing "docker build". The output can be copied into image being built.