【问题标题】:Unable to load type from assembly (C# Amazon lambda function)无法从程序集中加载类型(C# Amazon lambda 函数)
【发布时间】:2017-04-19 23:48:56
【问题描述】:

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

这是我的简单课程:

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 控制台。

在配置选项卡中,Handler被定义为:

MyTest::LambdaFunction::Handler

但是当我点击测试按钮时,这是返回的错误消息:

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

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

注意 2:我已经为 Handler 方法以及静态/实例类或方法或任何组合尝试了几种参数类型(Stream 和 String 是唯一受支持的,但没有自定义序列化程序),但都无济于事。

任何已经完成这项工作的人可以给我一些指导吗?

【问题讨论】:

    标签: c# amazon-web-services .net-core aws-lambda


    【解决方案1】:

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

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

    应该是:

    MyTest::MyTest.LambdaFunction::Handler
    

    【讨论】:

    • 我有同样的问题,添加命名空间没有帮助
    • @RomanAsanov 发布问题,展示您的代码。必须有区别。
    • 谢谢,有帮助!
    【解决方案2】:

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

    AssemblyName::NameSpace.ClassName::FunctionHandlerName
    

    【讨论】:

      【解决方案3】:

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

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

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

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

      $ export ASPNETCORE_ENVIRONMENT=Production
      $ dotnet run
      

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

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

      【讨论】:

        【解决方案4】:

        我也收到此错误消息。就我而言,我将我的函数称为“HelloLambda”,但随后复制了一些开始的代码:

        命名空间计算器。

        改成

        命名空间 HelloLambda

        解决了问题

        【讨论】:

          【解决方案5】:

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

          在我的情况下,解决方案是减少 AssemblyName::NameSpace.ClassName::FunctionHandlerName 组合的长度。此组合长度超过 128 个字符,并且 lambda 部署在 Windows 上。

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

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

          【讨论】:

            【解决方案6】:

            我的问题是架构不匹配。 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.
            

            【讨论】:

              猜你喜欢
              • 2012-05-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-09-07
              • 2016-09-27
              • 2010-11-30
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多