【发布时间】:2017-08-07 07:49:25
【问题描述】:
任何人都可以使用带有 C# .NET Core 的 AWS Lambda 来访问 AWS RDS 中的 MySQL 数据库吗?
我从 nuget 尝试了“Pomelo.EntityFrameworkCore.MySql”v1.1,并在将其转移到 AWS Lambda 之前在控制台应用程序 (.NET Core) 中进行了测试。
我正在运行 Visual Studio Professional Update 3 和 .NET Core 1.0.1 - VS 2015 Tooling Preview 2。
我的测试代码很简单...只是测试在 AWS RDS 中打开 MySQL 数据库,在控制台应用程序中一切正常,但显示错误“此平台不支持操作。”时移至 Lambda。
任何想法如何解决此问题或 Lambda C# 上的任何工作示例以访问 RDS MySQL 数据库?
下面是我的测试代码,
函数.cs
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");
}
}
project.json
{
"version": "1.0.0-*",
"buildOptions": {
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Amazon.Lambda.Core": "1.0.0*",
"Amazon.Lambda.Serialization.Json": "1.0.1",
"Amazon.Lambda.Tools": {
"type": "build",
"version": "1.3.0-preview1"
},
"Pomelo.EntityFrameworkCore.MySql": "1.1.0"
},
"tools": {
"Amazon.Lambda.Tools" : "1.3.0-preview1"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
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 )"
]
}
【问题讨论】:
标签: c# mysql amazon-web-services .net-core aws-lambda