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

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

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

仅使用最新的测试版/v2以及以下guide from Microsoft连接 SQL 数据库时出现错误。

我知道 integration of the dependencies 有一些差异。我的意思是文件“project.json”而不是“function.proj”的使用和结构

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

这是返回的错误。我提醒一下,这是测试版本,因为在我的实际项目中我也使用了 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");

最佳答案

v2 功能不再支持 ConfigurationManager,请参阅此 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>

关于c# - Azure Function beta/v2 - 集成依赖项 - SQL/MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51728599/

相关文章:

azure - 找不到部署 ASP.NET Core 7 Web API somepage.azurewebsites.net 页面

c# - 如何使用 CSS 包含隐藏的 DIV 而不会占用空间?

c# - Azure函数 "Could not load file or assembly"

c# - 将 utc 转换为本地日期时间

c# - 此版本的 SQL Server 不支持已弃用的功能 'INSERT NULL into TIMESTAMP columns'

c# - 您可以使用 SQL Azure 来实现简单的 ASP.NET 解决方案吗?

c# - 如何减少行数的边距大小

c# - string message = Regex.Replace(message, "\\", "") 有问题;

c# - 使用 block 吞噬异常

azure - 我一直在努力在 Azure Synapse Analytics 上找到任何相关文档,以使用 CLI 为 Azure KeyVault 提供链接服务