locked
Error using MySql connector with Xamarin RRS feed

  • Question

  • User31568 posted

    Hi,

    I am trying to use MySql plugin in Xamarin project.

    I have added the pliugin to all my PCL projects and the used:

    sql_connection = new MySqlConnection("Server=mysql.mydomain.com; Database=mydb; Uid=myuser; Pwd=mypassword;");
    
    try
    {
        sql_connection.Open();
    }
    catch (MySqlException ex)
    {
        DisplayAlert("Error", ex.Message, "Ok");
    }
    

    but I am getting below error:

    System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception. ---> System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConfiguration' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.PlatformNotSupportedException: Operation is not supported on this platform. at System.Configuration.ClientConfigPaths..ctor (System.String exePath, System.Boolean includeUserConfig) [0x00050] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.ClientConfigPaths.GetPaths (System.String exePath, System.Boolean includeUserConfig) [0x00018] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.ClientConfigurationHost.getConfigPaths () [0x0000a] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.ClientConfigurationHost.GetStreamName (System.String configPath) [0x0006d] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.ClientConfigurationHost.getIsAppConfigHttp () [0x00000] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp () [0x00006] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.ClientConfigurationSystem..ctor () [0x00051] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.ConfigurationManager.EnsureConfigurationSystem () [0x00024] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 --- End of inner exception stack trace --- at System.Configuration.ConfigurationManager.EnsureConfigurationSystem () [0x00060] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.ConfigurationManager.PrepareConfigSystem () [0x0000a] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at System.Configuration.ConfigurationManager.GetSection (System.String sectionName) [0x0000a] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 at MySql.Data.MySqlClient.MySqlConfiguration..cctor () [0x00000] in <575e720ed6c440559324a6291091586f>:0 --- End of inner exception stack trace --- at MySql.Data.MySqlClient.Replication.ReplicationManager..cctor () [0x0001e] in <575e720ed6c440559324a6291091586f>:0 --- End of inner exception stack trace --- at MySql.Data.MySqlClient.MySqlConnection.Open () [0x000b9] in <575e720ed6c440559324a6291091586f>:0

    Tuesday, June 5, 2018 11:35 PM

All replies

  • User2157 posted

    Clue is in 'Operation is not supported on this platform'. what platform are you running this on, and is the MySql plugin compatible?

    Wednesday, June 6, 2018 9:22 AM
  • User368877 posted

    I hope you are making a database administration app where the user has to provide his own login to the database? Otherwise it is a bad idea to put a MySQL connection in client code anyway. Anybody can read the username and password.

    Wednesday, June 6, 2018 12:23 PM
  • User31568 posted

    @AndyFlisher said: Clue is in 'Operation is not supported on this platform'. what platform are you running this on, and is the MySql plugin compatible?

    I am using .NET Standard Xamarin Forms

    I have added the plug in to all but did not get any error so i guess it's compatible?

    the plugin name is MySql.Data when you search Nuget

    Wednesday, June 6, 2018 12:37 PM
  • User2157 posted

    Hi @JassimRahma by platform I meant iOS / Mac / Windows / Android etc as not all Nugets and packages work on all platforms (even if they install), and some need client libraries installed (don't think that's the case here mind).

    Wednesday, June 6, 2018 12:49 PM
  • User31568 posted

    but how you know the compatibility if it;'s not mentioned in the nuget?

    https://www.nuget.org/packages/MySql.Data/

    Wednesday, June 6, 2018 4:23 PM
  • User65389 posted

    @JassimRahma I was on the project site (link right o top of the nuget site) and have not found a single information to Xamarin, iOS or Android, except your message, that it don't work with Xamarin :sunglasses:
    So... I would wait for an answer to your posting, but suspect, that it's not supported :wink:

    Wednesday, June 6, 2018 4:38 PM
  • User2157 posted

    Echoing the above, i’ve not found any examples that it does work in Xamarin, Mono yes, any again iOS, Android, Mac versions are different hence me asking (twice!) what platform? You also need to be sure that you’re on .net 4.5.2 or above according to the Nuget docs.

    Remember if this is a mobile app that direct connection to a database server from an app is normally frowned upon (use a web api front end) so a good chance it’s never been tested with Xamarin Movile platforms.

    If you really want to know for sure, get the arc Fromm Github and refernence that in your solution not the nuget, if anything can build and work it will be that.

    https://github.com/mysql/mysql-connector-net

    HTH

    Wednesday, June 6, 2018 4:45 PM
  • User384115 posted

    How do you solve the problem? I have the same problem.

    Wednesday, July 24, 2019 9:02 AM
  • User388106 posted

    I am using .NET Standard Xamarin Forms and mysql database . I added the plugin MySql.Data from nuget. This is the code which i used MySqlConnection sqlconnection; sqlconnection = new MySqlConnection("Server=localhost; Database=testdb; Uid=root; persistsecurityinfo = False; Pwd=admin;"); try { sql_connection.Open(); // here im getting the exception: } catch (MySqlException ex) { DisplayAlert("Error", ex.Message, "Ok"); }

    The exception is:

    Unhandled Exception:

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

    How to solve this.Please help me

    Thursday, August 8, 2019 5:33 AM
  • User389609 posted

    I have just come across this same problem.

    When using the MySql.Data package with Xamarin.Forms, my code works on iOS and successfully connects to the remote MySQL DB, but the same code throws a System.TypeInitializationException exception on Android devices at the point where I call the MySqlConnection .Open(); function.

    Can anyone help ? Is there any documentation available that definitively makes it clear whether Oracle's MySql.Data nuget package is compatible with Xamarin on Android or not?

    There is precious little helpful information available anywhere about connecting Xamarin directly to remote MySQL servers.

    BTW, I do know and accept that the recommended/best/safest way to connect to a remote database is by using a web service. However, that is not what I want to do. I want to connect directly to the remote MySQL DB server and can't help feeling that it must be possible with Xamarin using mainstream packages.

    My setup:

    Visual Studio 2019 (ver 16.3.2)

    MySql.Data by Oracle (ver 8.0.17) Xamarin.Forms (ver 4.2.0.709249) NETStandard.Library (ver 2.0.3) Xamarin.Android.Support.Core.Utils (ver 28.0.0.1) Xamarin.Essentials (ver 1.2.0)

    My code extract:

    using Xamarin.Forms;
    using MySql.Data.MySqlClient;
    ....
    string ConnectionString = "server=MySqlServer;uid=MyUserID;port=3306;pwd=MyPassword;OldGuids=True;";
    MySqlConnection cConn = new MySqlConnection(ConnectionString);
    
    try
    {
      cConn.Open(); // exception is thrown here for Xamarin.Android but runs ok for Xamarin.iOS
      cConn.Close();
      lblResult.Text = "Successfully Connected";
    }
    catch (Exception ex)
    {
      lblResult.Text = "Connection failed";
      Debug.WriteLine("ex.StackTrace" + ex.StackTrace.ToString());
    }
    

    The exception thrown in Android:

    **System.TypeInitializationException:** 'The type initializer for 'MySql.Data.MySqlClient.MySqlConfiguration' threw an exception.'
    
    {System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> 
    System.PlatformNotSupportedException: Operation is not supported on this platform.  
    
    at System.Configuration.ClientConfigPaths..ctor (System.String exePath, System.Boolean includeUserConfig) [0x00050] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.ClientConfigPaths.GetPaths (System.String exePath, System.Boolean includeUserConfig) [0x00018] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.ClientConfigurationHost.get_ConfigPaths () [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.ClientConfigurationHost.GetStreamName (System.String configPath) [0x0006d] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp () [0x00000] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp () [0x00006] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.ClientConfigurationSystem..ctor () [0x00051] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem () [0x00024] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0    --- End of inner exception stack trace ---  at System.Configuration.ConfigurationManager.EnsureConfigurationSystem () [0x00060] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.ConfigurationManager.PrepareConfigSystem () [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at System.Configuration.ConfigurationManager.GetSection (System.String sectionName) [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>
    :0   at MySql.Data.MySqlClient.MySqlConfiguration..cctor () [0x00000] in <cd136af85da143c0850cc4618e3d8d9f>
    :0 }
    

    Alternate MySQL Connector Packages

    I do note that there are other connector packages available on nuget that are supposed to work with Xamarin, but I need to know if Oracle's version is meant to work or not.

    • MySqlConnector nuget package has 4.6M downloads (as at 9-Oct-2019) and was last updated 5 days ago
    • Xamarin.MySql.Data nuget package has only 3,613 downloads and was last updated at begining of 2018, so doesn't look popular nor still maintained.
    Thursday, October 10, 2019 9:25 AM
  • User400593 posted

    As I recently commented on Stack Overflow, if you are using the MySQL Oracle package, then this part of the exception message you get:

    System.PlatformNotSupportedException: Operation is not supported on this platform.

    Means that this library doesn't work with Xamarin/Android due to architecture or OS incompatibilities. There is no compiler errors, but that Oracle MySQL package also raises other exceptions always related to architecture/OS when trying to execute its methods on a Xamarin/Android app, so I recently writed a message to Oracle asking them to include this info about incompatibilities on the README... All forums related to Xamarin and this library ends talking about its Android incompatibility issues.

    Here is the solution I use instead of the Oracle library. It's an alternative, up to date and very similar (and 100% working in Xamarin Android) package on NuGet: MySqlConnector

    Another alternative, very similar, but less updated and used, also available on NuGet: Xamarin.MySql.Data

    And there is also another package (With all doc in chinese and a weird/sofisticated way to build querys...) but it also works and receives frecuently updates. But better take a look first to doc and examples before trying to use: FreeSql

    I think this 3 libraries are the only working with Xamarin/Android, I hope it helps.

    Friday, April 16, 2021 10:20 AM