.net - 无法将独立的 .NET Core 3.0 应用程序部署到 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 Core 应用程序,其目标框架为 netcoreapp3.0,使用 Amazon.Lambda.AspNetCoreServer 和 Amazon.Lambda.RuntimeSupport 包。
当我运行时,dotnet lambda deploy-serverless
我收到错误:
NETSDK1031:不支持在不指定 RuntimeIdentifier 的情况下构建或发布自包含应用程序。请指定 RuntimeIdentifier 或将 SelfContained 设置为 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
它会在我的本地成功发布。
如果我使用相同的必需参数更新 msbulid-parameters 并运行,dotnet lambda deploy-serverless
我会得到:
...发布:MSBUILD:错误MSB1009:项目文件不存在。
我错过了什么?
解决方案
在此处挖掘 Amazon Lambda Dotnet CLI 工具的源代码后:https ://github.com/aws/aws-extensions-for-dotnet-cli/blob/6b3c0eaf04bb62995ca1c5dd71b0db7e564e8b74/src/Amazon.Lambda.Tools/LambdaDotNetCLIWrapper.cs )
我找到了以下构建 dotnet publish 命令的代码:
// 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,因为它是一个全栈 Web 应用程序。将以下参数添加到 msbuild 参数可解决此问题:-r rhel.7.2-x64
但 AWS Lambda 并不意味着处理全栈应用程序,静态文件无法正常服务,所以最好的办法是分解应用程序,让 Asp.Net Core lambda 项目只是一个纯 API。
推荐阅读
- haskell - monad-coroutine 的 MonadError 实例 任何暂停 Functor 的协程
- web-applications - HTTP 请求在“请求发送”上花费了太多时间(由 chrome devtools 提供)
- debugging - 在调试控制台中突出显示错误代码的问题 - Visual Studio Code
- jquery - 如何使选择器成为有效的选择器
- ionic-framework - Cordova 短信插件电容
- google-compute-engine - 没有内置 SQL 服务器的 GCE windows 服务器
- javascript - 自定义元素未在另一个角度应用程序中加载
- excel - 创建图表非常慢
- python - 如何获取所有准备好的打印内容的字符串
- python - 将一列中的值用于另一列