azure v2和mysql函数中的本地配置异常

9q78igpj  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(417)

[更新:在v2发布之前,azure功能平台中确实存在一个bug,在报告之后修复了该bug。]
在最新版本的visual studio 2017和最新版本的azure功能扩展中本地运行时,配置系统初始化似乎有问题。另外,这个项目使用的是azure功能平台的v2。
当我尝试打开mysql连接时,会引发一个异常:

connection.Open();

这是引发的异常:

System.Private.CoreLib: Exception while executing function: MyAzureFunctionName. 
MySql.Data: The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception. 
MySql.Data: The type initializer for 'MySql.Data.MySqlClient.MySqlConfiguration' threw an exception.    
System.Configuration.ConfigurationManager: Configuration system failed to initialize. 
System.Configuration.ConfigurationManager: Unrecognized configuration section system.serviceModel. (*LocalPath*\AppData\Local\Azure.Functions.V2.Cli\func.dll.config line 204).

它正在抱怨一个无法识别的配置部分。我确实有一个 local.settings.json 它包含一些应用程序设置。完整堆栈跟踪可在以下位置获得:

at MySql.Data.MySqlClient.Replication.ReplicationManager.IsReplicationGroup(String groupName)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at Internal.Functions.TestRemoteDatabaseConnection.Run(HttpRequest req, ILogger log, ExecutionContext context) in C:\Users\foo\Source\Repos\Functions\TestRemoteDatabaseConnection.cs:line xx

以下是完整的测试功能:

using System;
using System.Configuration;
using System.Data;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;

namespace Internal.Functions
{
    public static class TestRemoteDatabaseConnection
    {
        [FunctionName("TestRemoteDatabaseConnection")]
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
            HttpRequest req, ILogger log)
        {
            log.LogInformation("Testing a remote database connection...");

            var connectionString = Environment.GetEnvironmentVariable("DATABASE_CONNECTION");

            using (var connection = new MySqlConnection(connectionString))
            {
                var sqlStatement = "SELECT 1";
                try
                {
                    connection.Open();
                }
                catch (Exception e)
                {
                    log.LogError(e, "Unable to create connection");
                    throw;
                }

                var cmd = new MySqlCommand()
                {
                    CommandText = sqlStatement,
                    Connection = connection,
                    CommandType = CommandType.Text,
                    CommandTimeout = 10800 // 3 minutes
                };

                var result = cmd.ExecuteScalar();

                return new OkObjectResult($"Connection succeeded to remote database connection.  Result: {result}");
            }
        }
    }
}

来自 .csproj 文件:

<ItemGroup>
    <PackageReference Include="Analytics" Version="3.2.0-alpha" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.13" />
    <PackageReference Include="MySql.Data" Version="8.0.11" />
  </ItemGroup>

你知道会发生什么吗?

roejwanj

roejwanj1#

根据错误消息,似乎您使用configurationmanager从local.settings.json获取连接字符串。错误信息与mysql无关。
运行在运行时2.x(.net core)上的azure function v2,似乎configurationmanager不适用于它。
在您的例子中,您可以尝试读取环境变量。

string connectionString= Environment.GetEnvironmentVariable("ConnectionStrings:mysqlconnection");

或者你可以使用下面的代码来实现。

var builder = new ConfigurationBuilder()
                .SetBasePath(context.FunctionAppDirectory)
                .AddJsonFile("local.settings.json");
IConfigurationRoot configuration = builder.Build();
var connectionString = configuration["ConnectionStrings:mysqlconnection"];

local.settings.json文件

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "ConnectionStrings": {
    "mysqlconnection": "xxxxxxxx"
  }
}

试验结果:

相关问题