简体   繁体   中英

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

I am writing an Alexa skill in C# (.NET Core) to be run from an AWS Lambda function (which runs on Amazon Linux). When I compile the project ( dotnet publish ), I get the error:

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.

If I specify a Windows runtime (such as win10-x64 ), it will compile fine, however one of my dependencies (Google Sheets API) has problems at runtime due to being on the Linux-based Lambda. Using a Linux runtime (such as debian.8-x64) will cause the same compile error.

Why would the AWS SDK require a Windows runtime, when its own platform is Linux? I feel like I'm missing something obvious and will happily face palm the moment someone points the issue out to me.

My project.json files (main app, and .NET Core class library project):

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"
    }
  }
}

I found JeffRCorp posted on the AWS forums about having a similar issue , and their steps worked for me too.

First I edited project.json and moved the Microsoft.NETCore.App dependency into the frameworks section (although I'm not sure if this makes a difference). I also removed the runtimes section. Final 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"
        }
      }
    }
  }
}

I then ran dotnet lambda package which built a .zip file in the bin/Release/netcoreapp1.0 directory. Uploaded this to AWS Lambda via the AWS Console and voila!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM