0

I have a simple custom C# class library that I use in my WPF projects for connecting to a MySQL database. Everything works as expected there.

However, I'm trying the same code to connect to the database from within Blazor and getting:

The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception

Everything compiles as expected with no errors, but when I run and try to call any method on my database object the exception is thrown. I'm not sure what the error means.

My library references the MySql.Data core class library from Oracle.

I'm guessing something might need to be setup differently to work in Blazor but I'm not sure.

Any ideas? Thanks!

Here is the (simplified) code from my library to create a database connection...

public class MySQLDatabase
{
    /* Properties */    

    public MySqlConnection MySqlConnection { get; set; }

    public ConnectionState ConnectionState { get; set; }

    /* Constructor */

    public MySQLDatabase(string databaseName, string ipAddress, string user, string password, int port = 3306)
    {
        MySqlConnection = new MySqlConnection(connectionString: $"server={ipAddress};user={user};database={databaseName};port={port};password={password};");
    }


    /* Methods */

    public void OpenConnection()
    {
        try
        {
            if (ConnectionState == ConnectionState.Closed)
            {
                MySqlConnection.Open();
            }
        }
        catch (Exception ex)
        {
            // Handle exception
        }
    }

    public void CloseConnection()
    {
        try
        {
            MySqlConnection.Close();
        }
        catch (Exception ex)
        {
            // Handle exception
        }
    }

    public bool TestConnection()
    {
        bool canConnect = false;

        OpenConnection();

        if (ConnectionState == ConnectionState.Open)
        {
            canConnect = true;
        }

        CloseConnection();

        return canConnect;
    }
}

And an example of how that's used...

MySQLDatabase database = new MySQLDatabase(databaseName: "MyDatabase", ipAddress: "999.999.999.999", user: "MyUsername", password: "MyPassword");

if (database.TestConnection() == false) {
    // Do something
} else {
    // Do something else
}

Running TestConnection or any other method gives me the "Replication" error.

n0kx
  • 61
  • 7
  • @HenkHolterman Wasm. Damn. How come? What's the best way to connect to a MySQL database via Wasm? – n0kx May 28 '20 at 22:36
  • 1
    The reasons it won't work are explained here: https://stackoverflow.com/a/60171574/23633 – Bradley Grainger May 28 '20 at 23:32
  • 1
    Oracle's MySql.Data library doesn't have good support for non-.NET Framework platforms. If you switched to https://www.nuget.org/packages/MySqlConnector/ you would get further, but still eventually be blocked by the problem that arbitrary TCP connections can't be opened from WASM. I'm going to close this question as a duplicate of that one since that's fundamentally what you're trying to do. – Bradley Grainger May 28 '20 at 23:55
  • @BradleyGrainger So I'm guessing I could use my database code on Blazor Server and then communicate with Blazor Server via SignalIR? – n0kx May 29 '20 at 01:59
  • @n0kx I must admit I'm not a Blazor expert, but running the MySQL client on the Server sounds like the right approach. – Bradley Grainger May 29 '20 at 05:25

0 Answers0