无法将独立的 .NET Core 3.0 应用程序部署到 AWS Lambda

Cannot deploy a self-contained .NET Core 3.0 application to AWS Lambda

我已按照本指南将 .NET Core 3.0 应用程序部署到 AWS Lambda:

https://aws.amazon.com/blogs/developer/net-core-3-0-on-lambda-with-aws-lambdas-custom-runtime/

我的 serverless.template 和 aws-lambda-tools-defaults.json 文件是本文的副本。

我的应用程序是一个 ASP.NET 核心应用程序,目标框架为 netcoreapp3.0,使用 Amazon.Lambda.AspNetCoreServer 和 Amazon.Lambda.RuntimeSupport 包。

当我 运行 dotnet lambda deploy-serverless 我得到错误:

NETSDK1031: It is not supported to build or publish a self-contained application without specifying a RuntimeIdentifier. Please either specify a RuntimeIdentifier or set SelfContained to false.

我看到的每个示例都显示了 msbuild 参数:

"msbuild-parameters": "--self-contained true /p:AssemblyName=bootstrap"

但显然,我可以从命令行尝试 dotnet publish --self-contained true 命令,但我得到了同样的错误 (NETSDK1031)。这应该如何工作?

如果我更改命令 运行 dotnet publish --self-contained true -f netcoreapp3.0 -r linux-x64 它会在我的本地成功发布。

如果我用相同的必需参数和 运行 dotnet lambda deploy-serverless 更新 msbulid-parameters,我得到:

... publish: MSBUILD : error MSB1009: Project file does not exist.

我错过了什么?

在此处深入了解 Amazon Lambda Dotnet CLI 工具的源代码后:https://github.com/aws/aws-extensions-for-dotnet-cli/blob/6b3c0eaf04bb62995ca1c5dd71b0db7e564e8b74/src/Amazon.Lambda.Tools/LambdaDotNetCLIWrapper.cs)

我找到了以下构建 dotnet 发布命令的代码:

// If you set the runtime to RUNTIME_HIERARCHY_STARTING_POINT it will trim out the Windows and Mac OS specific dependencies but Razor view precompilation
// will not run. So only do this packaging optimization if there are no Razor views.
if (Directory.GetFiles(fullProjectLocation, "*.cshtml", SearchOption.AllDirectories).Length == 0)
{
    arguments.Append($" -r {LambdaConstants.RUNTIME_HIERARCHY_STARTING_POINT}");

    if (msbuildParameters == null ||
        msbuildParameters.IndexOf("--self-contained", StringComparison.InvariantCultureIgnoreCase) == -1)
    {
        arguments.Append(" --self-contained false ");
    }

    if (string.IsNullOrEmpty(msbuildParameters) ||
        !msbuildParameters.Contains("PreserveCompilationContext"))
    {
        _logger?.WriteLine("... Disabling compilation context to reduce package size. If compilation context is needed pass in the \"/p:PreserveCompilationContext=false\" switch.");
        arguments.Append(" /p:PreserveCompilationContext=false");
    }
}

我的网站确实包含一个 CSHTML,因为它是一个全栈网络应用程序。将以下参数添加到 msbuild 参数可解决此问题:-r rhel.7.2-x64

但是 AWS Lambda 并不意味着处理全栈应用程序,静态文件将无法正常服务,所以最好的办法是分解应用程序,以便 Asp.Net 核心 lambda 项目只是一个纯粹的 API.

我遇到了同样的问题,我将 /p:AssemblyName=bootstrap 移到了我的 .csproj 文件中。见附图。