[Update: There was indeed a bug in the Azure Functions platform before release of v2 that was fixed after reporting it.]
There seems to be a problem with the configuration system initializing when running locally in the latest version of Visual Studio 2017 and the latest version of the Azure Functions extension. Additionally, this project is using V2 of the Azure Functions platform.
An exception is being thrown as soon as I'm attempting to open a MySQL connection with:
connection.Open();
This is the exception that gets raised:
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).
It's complaining about an unrecognized configuration section. I do have a local.settings.json file and it contains a few App Settings. The full stack trace is available here:
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
Here is the full test function:
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}");
}
}
}
}
Package references snippet from the .csproj file:
<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>
Any ideas what could be going on?
