gpt4 book ai didi

mysql - Azure Functions v2 和 MySQL 中的本地配置异常

转载 作者:行者123 更新时间:2023-11-29 02:41:51 24 4
gpt4 key购买 nike

[更新:在发布 v2 之前,Azure Functions 平台中确实存在一个错误,在报告后已修复。]

在最新版本的 Visual Studio 2017 和最新版本的 Azure Functions 扩展中本地运行时,配置系统初始化似乎存在问题。此外,该项目正在使用 Azure Functions 平台的 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>

知道会发生什么吗?

最佳答案

根据错误消息,您似乎使用ConfigurationManager 从local.settings.json 获取连接字符串。错误信息与mysql无关。

在运行时 2.x(.net core) 上运行的 Azure 函数 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"
}
}

测试结果:

enter image description here

关于mysql - Azure Functions v2 和 MySQL 中的本地配置异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50142785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com