2

[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?

2
  • 1
    It looks as though the MySQL client is using ConfigurationManager: github.com/mysql/mysql-connector-net/blob/… Commented May 10, 2018 at 21:39
  • i have same problem, can you reference me any link which have any solution. Commented Sep 13, 2019 at 8:26

1 Answer 1

1

According to the error message, it seems that you use ConfigurationManager to get the connection string from the local.settings.json. The error information is not related to mysql.

Azure function v2 running on runtime 2.x(.net core),it seems that ConfigurationManager is not working for it.

In your case, you could try to read enviroment variables instead.

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

Or you could use the following code to do that.

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"
  }
}

Test Result:

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Tom! I should have mentioned, I wasn't using ConfigurationManager :( I call an environment variable before this point for the database connection. var connectionString = Environment.GetEnvironmentVariable("DATABASE_CONNECTION"); I'm not sure what in the MySQL library would want to be getting something from the configuration system.
If there is demo code to reproduce it will be more helpful.
My hunch is that this is more of a local environment issue. Two dev machines are having this issue but hosting them in Azure are not having a problem. It's very strange.
I added a full snippet to my original question to see if that will help.
Sorry for about that. Based on my test, if I use it in the Azure function V2 I also could reproduce it. If test it with Azure function V1 or .net core/.net platform, it works correctly. I recommand that you could report it to azure function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.