简体   繁体   English

是否有人在C#AWS Lambda函数或AWS Linux EC2实例中成功运行SQL Server连接器?

[英]Has anyone successfully run a SQL Server connector within a C# AWS Lambda function, or AWS Linux EC2 instance?

I've been working all night trying to get a SQL Server connection from either a Lambda function or an AWS Linux EC2 instance with no luck. 我整夜都在努力从Lambda函数或AWS Linux EC2实例中获取SQL Server连接,但没有成功。

I started by getting .NET Core running in Windows and getting a successful write/query working there using System.Data.SqlClient. 首先,让.NET Core在Windows中运行,然后使用System.Data.SqlClient成功地进行写入/查询。 Then I attempted to move it into an AWS Lambda function. 然后,我尝试将其移至AWS Lambda函数中。 It doesn't appear that there are any issues with libraries, but rather I always get this error when trying to open the database connection: 似乎库没有任何问题,但是在尝试打开数据库连接时我总是会收到此错误:

Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. 未处理的异常:System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server) (提供程序:TCP提供程序,错误:40-无法打开与SQL Server的连接)

I then decided to get the same code working in Ubuntu Linux (16.04) and was able to successfully write/query data to a SQL Server database there. 然后,我决定让相同的代码在Ubuntu Linux(16.04)中工作,并能够在那里成功地向SQL Server数据库写入/查询数据。 Next I attempted the exact same code running in a AWS Linux AMI (since these are what AWS Lambda runs on), and received the exact same error code above. 接下来,我尝试在AWS Linux AMI上运行完全相同的代码(因为这些是运行AWS Lambda的代码),并收到上面的完全相同的错误代码。 Then I attempted to give it a shot using Entity Framework and got the exact same error. 然后,我尝试使用Entity Framework试一试,并得到了完全相同的错误。

Has anyone been successful doing this?? 有人成功做到了吗? If so, I'd love to hear more about your specific approach. 如果是这样,我很想听听您的具体做法。 Or, also, does anyone know if this isn't possible? 或者,还有谁知道这是不可能的吗?

I'll paste the code snippet below, even though I don't believe this is the problem (same code works in Ubuntu but not in AWS Linux). 即使我不相信这是问题所在,我也会在下面粘贴代码片段(相同的代码在Ubuntu中可用,但在AWS Linux中不可用)。 Note: this is a mock database just to test. 注意:这是一个模拟数据库,仅供测试。 It seems to be a driver problem, but hard to be sure. 这似乎是一个驱动程序问题,但很难确定。

using System;
using System.IO;
using System.Data.SqlClient;

namespace CSharpLambdaFunction
{
    public class Program
    {
        public static void Main() {
            LambdaHandler.myHandler();
        }
    }

    public class LambdaHandler
    {
        public static void myHandler()
        {
            SqlConnection myConnection = new SqlConnection("user id=usernamehere;" +
                                        "password=passwordhere;server=serveraddresshere;" +
                                        "database=test; " +
                                        "connection timeout=30");
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
            int secondsSinceEpoch = (int)t.TotalSeconds;

            SqlCommand myCommand = new SqlCommand("INSERT INTO dbo.Table_1 (test1, test2) " +
                                        "Values ('testing', " + secondsSinceEpoch + ")", myConnection);
            myCommand.ExecuteNonQuery();

            try
            {
                SqlDataReader myReader = null;
                myCommand = new SqlCommand("select * from dbo.Table_1",
                                                            myConnection);
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    Console.WriteLine(myReader["test1"].ToString());
                    Console.WriteLine(myReader["test2"].ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

I found the issue to be that the EC2 SQL Server instance didn't have the correct inbound rules! 我发现问题是EC2 SQL Server实例没有正确的入站规则! The code above is functional. 上面的代码可以正常工作。 Just for example purposes, see below for the corresponding project.json: 仅出于示例目的,请参见下面的相应project.json:

{
  "version": "1.0.0-*",
  "dependencies": {},
  "buildOptions": {
    "warningsAsErrors": true,
    "emitEntryPoint": true
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        },
        "System.Data.SqlClient": {
          "version": "4.1.0-*"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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