Azure Function beta / v2-集成依赖-SQL / MySql

时间:2018-08-07 13:58:22

标签: c# azure azure-sql-database azure-functions azure-functions-runtime

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

仅使用最新的 beta版本/ 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");

1 个答案:

答案 0 :(得分:3)

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

  

在2.0中,我们已移至新的ASP.NET Core配置模型。我们打算支持绑定到将提供类似API的IConfiguration实例,但是与此同时,解决方法是读取环境变量,而不是依赖ConfigurationManager及其API。

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

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

更新,用于MySql Connection

转到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>