如何通过AWS Lambda C#访问MySQL服务器?
有人能使用AWS Lambda和C#.NET内核访问AWS RDS中的MySQL数据库吗 我尝试了nuget的“Pomelo.EntityFrameworkCore.MySql”v1.1,并在控制台应用程序(.NET Core)中进行了测试,然后将其转换为AWS Lambda 我正在运行Visual Studio Professional Update 3和.NET Core 1.0.1-VS 2015工具预览2 我的测试代码很简单…只需在AWS RDS中测试打开MySQL数据库,在控制台应用程序中一切正常,但在移动到Lambda时显示错误“此平台不支持操作。” 有没有办法解决这个问题或者在Lambda C#上的任何工作示例来访问RDS MySQL数据库 下面是我的测试代码 函数.cs如何通过AWS Lambda C#访问MySQL服务器?,c#,mysql,amazon-web-services,.net-core,aws-lambda,C#,Mysql,Amazon Web Services,.net Core,Aws Lambda,有人能使用AWS Lambda和C#.NET内核访问AWS RDS中的MySQL数据库吗 我尝试了nuget的“Pomelo.EntityFrameworkCore.MySql”v1.1,并在控制台应用程序(.NET Core)中进行了测试,然后将其转换为AWS Lambda 我正在运行Visual Studio Professional Update 3和.NET Core 1.0.1-VS 2015工具预览2 我的测试代码很简单…只需在AWS RDS中测试打开MySQL数据库,在控制台应用程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization;
using Microsoft.EntityFrameworkCore;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializerAttribute(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AWSLambda1
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(ILambdaContext context)
{
Console.WriteLine("Lambda starting");
using (var mySQLcontext = new MyContext())
{
// Create database
mySQLcontext.Database.EnsureCreated();
}
return "Lambda stopped";
}
}
public class MyContext : DbContext
{
//public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseMySql(@"Server=your_dbsvr_host;database=testDB;uid=admin;pwd=password123");
}
}
Lambda错误响应
{
"errorType": "PlatformNotSupportedException",
"errorMessage": "Operation is not supported on this platform.",
"stackTrace": [
"at System.Runtime.InteropServices.OSPlatform.get_Windows()",
"at MySql.Data.Serialization.ConnectionSettings..ctor(MySqlConnectionStringBuilder csb)",
"at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)",
"at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlRelationalConnection.get_DbConnection()",
"at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlRelationalConnection.Open()",
"at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlDatabaseCreator.Exists(Boolean retryOnNotExists)",
"at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()",
"at AWSLambda1.Function.FunctionHandler(ILambdaContext context)",
"at lambda_method(Closure , Stream , Stream , ContextInfo )"
]
}
问题是project.json文件中缺少“runtime”规范。这不仅限于运行在Lambda中的.Net Core,而且在所有.Net Core应用程序中都是必需的 语法如下所示:
"runtimes": {
"win10-x64": {}
}
此外,下面是Microsoft关于运行时标识符的文档:,其中包含有效运行时的列表。“win10-x64”运行时对于Lambda来说很好,它看起来像是在基础库中运行的,该库于2017年1月修复 将
Pomelo.EntityFrameworkCore.MySql
包更新为1.1.1将引入新版本的MySqlConnector并修复此错误
"runtimes": {
"win10-x64": {}
}