c# - AWS Lambda 和 .NET Core - 使用 Linux 运行时?

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

我正在用 C# (.NET Core) 编写 Alexa 技能,以便从 AWS Lambda 函数(在 Amazon Linux 上运行)运行。当我编译项目 (dotnetpublish) 时,出现错误:

Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10- x64, win81-x64, win8-x64, win7-x64'. Possible causes:

  1. The project has not been restored or restore failed - run dotnet restore
  2. The project does not list one of 'win10-x64, win81-x64, win8-x64, win7-x64' in the 'runtimes' section.
  3. You may be trying to publish a library, which is not supported. Use dotnet pack to distribute libraries.

如果我指定 Windows 运行时(例如 win10-x64),它将正常编译,但是我的依赖项之一(Google Sheets API)由于位于 Linux 上而在运行时出现问题 -基于 Lambda。使用Linux运行时(例如debian.8-x64)将导致相同的编译错误。

当 AWS SDK 自己的平台是 Linux 时,为什么它需要 Windows 运行时?我觉得我错过了一些明显的东西,当有人向我指出问题时,我会很高兴地面对手掌。

我的project.json文件(主应用程序和.NET Core类库项目):

AlexaProj

{
  "version": "1.0.0-*",
  "buildOptions": {
  },

  "dependencies": {
    "Microsoft.NETCore.App": "1.1.1",
    "Amazon.Lambda.Core": "1.0.0*",
    "Amazon.Lambda.Serialization.Json": "1.0.1",
    "Amazon.Lambda.Tools": {
      "type": "build",
      "version": "1.3.0-preview1"
    },
    "Slight.Alexa.Core": "1.0.10-beta",
    "AlexaProjLib": "1.0.0-*"
  },

  "tools": {
    "Amazon.Lambda.Tools": "1.3.0-preview1"
  },

  "runtimes": {
    "win10-x64": { }  <--- Compiles, but fails at runtime
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

AlexaProjLib

{
  "version": "1.0.0-*",

  "dependencies": {
    "Google.Apis": "1.21.0",
    "Google.Apis.Core": "1.21.0",
    "Google.Apis.Oauth2.v2": "1.21.0.672",
    "Google.Apis.Sheets.v4": "1.21.0.798",
    "NETStandard.Library": "1.6.1"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

最佳答案

我找到了JeffRCorp posted on the AWS forums about having a similar issue ,他们的步骤也对我有用。

首先,我编辑了 project.json 并将 Microsoft.NETCore.App 依赖项移至 frameworks 部分(尽管我不确定如果这有影响的话)。我还删除了运行时部分。最终的project.json:

{
  "version": "1.0.0-*",
  "buildOptions": {
  },

  "dependencies": {
    "Amazon.Lambda.Core": "1.0.0*",
    "Amazon.Lambda.Serialization.Json": "1.0.1",
    "Amazon.Lambda.Tools": {
      "type": "build",
      "version": "1.3.0-preview1"
    },
    "Slight.Alexa.Core": "1.0.10-beta",
    "AlexaProjLib": "1.0.0-*"
  },

  "tools": {
    "Amazon.Lambda.Tools": "1.3.0-preview1"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50",
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.1"
        }
      }
    }
  }
}

然后我运行了 dotnet lambda package,它在 bin/Release/netcoreapp1.0 目录中构建了一个 .zip 文件。通过 AWS 控制台将其上传到 AWS Lambda,瞧!

关于c# - AWS Lambda 和 .NET Core - 使用 Linux 运行时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868476/

相关文章:

制表符分隔文件中的 C# FileHelpers 空值

c# - 循环遍历 appConfig 文件的不同部分

linux - 在 Elastic Beanstalk 上为 FFMPEG 启用 libmp3lame

amazon-web-services - .ebextensions 在 elasticbeanstalk 上使用 Docker

asp.net - 使用 .net Framework 4 及更高版本(而不是 .NET Core)的微服务

c# - 我需要将线程访问同步到 int 吗?

python - 如何将 Python Lambda 函数集成到 AWS Amplify 的管道中

docker - 将Windows共享安装到Kubernetes容器中

c# - ASP.NET Core (.NET Core) 和 ASP.NET Core (.NET Framework) 的区别

c# - 将 C# 代码与 roslyn : comment disappears 合并