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

AWS Lambda and .NET Core - Using a Linux Runtime?

我正在用 C# (.NET Core) 编写 Alexa 技能,使其成为来自 AWS Lambda 函数的 运行(亚马逊上的 运行s Linux)。当我编译项目(dotnet publish)时,出现错误:

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 需要 Windows 运行 时间,而它自己的平台是 Linux?我觉得我遗漏了一些明显的东西,当有人向我指出问题时,我会很高兴地面对掌心。

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

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 依赖项移动到 框架中 部分(虽然我不确定这是否有所作为)。我还删除了 runtimes 部分。决赛 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 包bin/Release/netcoreapp1.0 中构建了一个 .zip 文件目录。通过 AWS 控制台将其上传到 AWS Lambda,瞧!