【问题标题】:How to access MySQL server via AWS Lambda C#?如何通过 AWS Lambda C# 访问 MySQL 服务器?
【发布时间】: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


    【解决方案1】:

    问题是您的 project.json 文件中缺少“运行时”规范。这不仅仅针对在 Lambda 中运行的 .Net Core,而是在所有 .Net Core 应用程序中都是必需的。

    语法如下:

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

    此外,这里是 Microsoft 关于运行时标识符的文档:.Net Core RID's,其中包含有效运行时列表。 “win10-x64”运行时适用于 Lambda

    【讨论】:

      【解决方案2】:

      您似乎遇到了底层 MySqlConnector 库中的 this bug,该库已于 2017 年 1 月修复。

      Pomelo.EntityFrameworkCore.MySql 包更新到 1.1.1 将引入更新版本的 MySqlConnector 并修复此错误。

      【讨论】:

        猜你喜欢
        • 2018-04-18
        • 2016-03-17
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 2021-01-21
        • 2015-06-14
        • 2018-10-31
        • 2016-05-17
        相关资源
        最近更新 更多