2017-03-16 93 views
0

任何人都可以在AWS RDS中使用AWS Lambda和C#.NET Core來訪問MySQL數據庫?如何通過AWS Lambda C#訪問MySQL服務器?

我試過了「Pomelo.EntityFrameworkCore.MySql」v1.1,它已經在控制檯應用程序(.NET Core)中進行了測試,然後再轉換到AWS Lambda。

我在運行Visual Studio專業版更新3和.NET核心1.0.1 - 2015年VS工具預覽2.

我的測試代碼很簡單...只是測試在AWS RDS打開MySQL數據庫,事情的來龍去脈在控制檯應用程序中沒有問題,但在移動到Lambda時顯示錯誤「此平臺不支持操作。」。

任何想法如何解決這個或在Lambda C#上訪問RDS MySQL數據庫的任何工作示例?

下面是我的測試代碼,

Function.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)" 
    ] 
} 

回答

0

問題是您在project.json文件中缺少「運行時」規範。這不僅限於在Lambda中運行的.Net Core,而且在所有.Net Core應用程序中都是必需的。

的語法看起來像:

"runtimes": { 
     "win10-x64": {} 
    } 

而且,這裏是微軟的運行時標識符文件:.Net Core RID's,其有效運行時間的列表。在「win10-64」的運行時間爲罰款LAMBDA

0

它看起來像你在基礎MySqlConnector libary,這是固定在一月2017

更新Pomelo.EntityFrameworkCore.MySql包1.1.1運行到this bug會引入更新版本的MySqlConnector並修復此錯誤。