Azure 函数 beta/v2 - 集成依赖项 - SQL/MySql

Azure Function beta/v2 - Integration Dependensies - SQL/MySql

我正在使用 Azure Functions 的测试版,因为我正在使用 Graph API 并且我想连接数据库 (MySQL/SQL)。

只是使用最新的 beta version/v2 和以下 guide from Microsoft 来连接 SQL 数据库我得到一个错误。

我知道 integration of the dependencies 存在一些差异。我的意思是文件“project.json”而不是“function.proj

的使用和结构

我必须做些什么才能让它发挥作用?

这是返回的错误。我提醒它是 beta 版本,因为在我的真实项目中我也使用 Graph Api:

2018-08-08T06:43:59  Welcome, you are now connected to log-streaming service.
2018-08-08T06:44:06.613 [Information] Executing 'Functions.TimerTriggerCSharp1' (Reason='This function was programmatically called via the host APIs.', Id=a3eb2b63-b336-4f56-bc3e-a1862b8f2d06)
2018-08-08T06:44:06.671 [Information] C# Timer trigger function executed at: 8/8/2018 6:44:06 AM
2018-08-08T06:44:06.786 [Error] System.Private.CoreLib: Exception while executing function: Functions.TimerTriggerCSharp1. System.Private.CoreLib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__-1020707796: Object reference not set to an instance of an object.
2018-08-08T06:44:06.906 [Error] Executed 'Functions.TimerTriggerCSharp1' (Failed, Id=a3eb2b63-b336-4f56-bc3e-a1862b8f2d06)
2018-08-08T06:45:07.294 [Information] Executing 'Functions.TimerTriggerCSharp1' (Reason='Timer fired at 2018-08-08T06:45:07.2936684+00:00', Id=2a28db1e-f70f-4919-b7c9-078d39fb12a6)
2018-08-08T06:45:07.300 [Information] C# Timer trigger function executed at: 8/8/2018 6:45:07 AM
2018-08-08T06:45:07.303 [Error] System.Private.CoreLib: Exception while executing function: Functions.TimerTriggerCSharp1. System.Private.CoreLib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__-1020707796: Object reference not set to an instance of an object.
2018-08-08T06:45:07.893 [Error] Executed 'Functions.TimerTriggerCSharp1' (Failed, Id=2a28db1e-f70f-4919-b7c9-078d39fb12a6)

这是我的 "run.csx" 文件:

#r "System.Configuration"
#r "System.Data"


using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Threading.Tasks;

public static void Run(TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

    var str = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(str))
    {
        conn.Open();
        //Just a test
        var text = "UPDATE Persons SET FirstName = 'Alfred Schmidt', LastName= 'Frankfurt'";

        using (SqlCommand cmd = new SqlCommand(text, conn))
        {
            // Execute the command and log the # rows affected.
            //var rows = await cmd.ExecuteNonQueryAsync();
            //log.Info($"{rows} rows were updated");
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        }
    }
}

更新

我正在尝试连接 MySQL 数据库而不是 SQL,但出现以下错误:

2018-08-08T12:54:46.569 [Error] run.csx(15,27): error CS0246: The type or namespace name 'MySqlConnection' could not be found (are you missing a using directive or an assembly reference?) 

这些是我的依赖项:

#r "System.Configuration" 
#r "System.Data" 
using System; 
using System.Configuration; 
using MySql.Data.MySqlClient; 
using System.Threading.Tasks; 

var str = Environment.GetEnvironmentVariable("MYSQLCONNSTR_MySqlConnection");

ConfigurationManager 在 v2 功能中不再受支持,请参阅此 relate comment

In 2.0, we have moved to the new ASP.NET Core configuration model. We intend to support binding to an IConfiguration instance that will provide a similar API, but in the meantime, the workaround is to read environment variables as opposed to relying on the ConfigurationManager and its APIs.

因此您可以使用以下语句来获取连接字符串。前缀 SQLAZURECONNSTR_ 由 Azure 为连接字符串部分下设置的 SQLAzure 连接字符串添加。

var str = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_SqlConnection");

更新 MySql 连接

转到https://<functionappname>.scm.azurewebsites.net/dev/wwwroot/,它显示了函数应用程序的内容。右键单击您的函数文件夹,新建文件,如下所示创建 function.proj。添加此依赖文件导入相关程序集供您参考。

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="MySql.Data" Version="8.0.12" />
    </ItemGroup>
</Project>