mysql-net / MySqlConnector

MySQL Connector for .NET
https://mysqlconnector.net
MIT License
1.39k stars 332 forks source link

Issue when using MySQLConnector 2.0+ with COM applications #1512

Open sbs-mvorkapich opened 2 weeks ago

sbs-mvorkapich commented 2 weeks ago

Software versions MySqlConnector version: 2.0.0 to 2.3.7 Server type (MySQL, MariaDB, Aurora, etc.) and version: MariaDB 10.10.2 .NET version: 3.5 to 4.8

Describe the bug I wrote a small DLL several years ago to connect really old VB6/C++ applications to Maria DB as an interim step while we convert these programs to C#, which is a very long process. This has been working very well and all our VB6/C++ applications can connect to Maria DB without issue One of our developers was updating the DLL and noticed the version of MySQLConnector was 1.3.14 This developer decided to update to the latest version, 2.3.7 This appeared to work and the DLL compiled. Using our test code, which is in C#, it can see and use the DLL without issue. However, when compiled to a TLB file, we started getting issues. These only show up when accessing the DLL through COM via one of the older applications. We tried several versions all the way back to 2.0 and the most recent version that works correctly is 1.3.14

Exception 2.3.7 gives this exception The type initializer for 'MySqlConnector.Core.ConnectionPool' threw an exception at MySqlConnector.Core.ConnectionPool.GetPool(STring connectionString, MySqlConnectorLoggingConfiguration loggingConfiguration, Boolean createIfNotFound) at MySqlConnector.MySqlConnection. d__29.MoveNext()

There is more but I'm having to type this by hand since I can only get it to show up in a message box popped up by the old COM application

One thing to note: reverting to an older version of MySqlConnector, such as 2.0, gives a different message. The message in that case is that the System.Diagnostics.DiagnosticSource is the incorrect version. It's looking for 6.0.0. However, when I manage to point it at 6.0.0 it then complains about another dependency and another and another. Again, only in the compiled .TLB file. Running from C# gives no errors.

Code sample

/* A concise code sample to reproduce the bug */
            try
            {
                MessageBox.Show("Creating connection");
                mySqlConn = new MySqlConnection();
                MessageBox.Show("Adding connection string");
                mySqlConn.ConnectionString = connectionString;
                MessageBox.Show("Opening connection");
                mySqlConn.Open(); <-- Part that fails
            }
            catch (Exception ex)
            {
                //I added all these message boxes so I could see the error from within the COM      
                //application
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
                OnErrorOccurred(ex);
                OnConnected(false);
                return false;
            }

Expected behavior I expect the connection to open and no errors to be thrown. Using 1.3.14, this is what happens. Running from C#, it all works.

Additional context I can provide the connection string, minus private details, if that would help. Settings inside Visual Studio: Project->Properties->Application->Assembly Information->Make assembly COM-Visible = true Project->Properties->Build->Register for COM interop =true Project->Properties->Signing->Sign the assembly = true with pfx file provided

At the top of the C# class module: [Guid("179C71E6-C198-469D-83E5-AA3223F6F0C9")] [ClassInterface(ClassInterfaceType.AutoDual)] [ComVisible(true)]

As stated before, all this works as expected if MySqlConnector version is less than 2.0.

bgrainger commented 2 weeks ago

There is more but I'm having to type this by hand since I can only get it to show up in a message box popped up by the old COM application

Try pressing Ctrl+C to copy the text (from a standard Windows message box) to the clipboard (when it has the keyboard focus).

bgrainger commented 2 weeks ago

Without more detail, it sounds like a binding redirect issue. The steps in this AI response sound reasonable to me; I would recommend going through it as a list of troubleshooting ideas, particularly Fusion Log Viewer and Binding Redirects. https://chatgpt.com/share/66e9e9c3-734c-8002-b9be-186495c55ae0