[更新:在发布v2之前,Azure功能平台确实存在一个错误,该错误在报告后已修复。]
在最新版本的Visual Studio 2017和最新版本的Azure Functions扩展中本地运行时,配置系统初始化似乎存在问题。此外,该项目使用的是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>
任何想法可能会发生什么?
答案 0 :(得分:1)
根据错误消息,您似乎使用 ConfigurationManager 从local.settings.json获取连接字符串。错误信息与mysql无关。
在运行时2.x(.net核心)上运行的Azure函数v2,似乎ConfigurationManager不能正常工作。
在您的情况下,您可以尝试阅读环境变量。
profile.bud.buddies.moredata
或者您可以使用以下代码执行此操作。
nested
<强> local.settings.json 强>
string connectionString= Environment.GetEnvironmentVariable("ConnectionStrings:mysqlconnection");
测试结果: