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.