Problem with connecting to MySQL/MongoDB/Postgres etc. database "Could not load file or assembly.."

Viewed 37

I am trying to connect to a MySQL database. Unfortunately I always get errors like this:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=5.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Das System kann die angegebene Datei nicht finden.
File name: 'System.Diagnostics.DiagnosticSource, Version=5.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
   at MySqlConnector.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at MySqlConnector.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken)
   at MySqlConnector.MySqlConnection.Open() in /_/src/MySqlConnector/MySqlConnection.cs:line 373
   at AdminTools.AdminToolsDatabase.Connect() in C:\Users\Milan\source\repos\AdminTools_Server\AdminTools_Server\AdminToolsDatabase.cs:line 30

"Das System kann die angegebene Datei nicht finden." should mean something like "The system cannot find the specified file."

using System;
using MySqlConnector;

namespace AdminTools
{
    class AdminToolsDatabase
    {
        private static string connectStr = @"server=127.0.0.1;uid=Adminii;pwd=123;database=server_db";
        private static MySqlConnection cnn = null;
        public AdminToolsDatabase()
        {
            cnn = new MySqlConnection(connectStr);
            Connect();
            Console.WriteLine("Connection established");
        }

        private bool Connect()
        {

            if(cnn != null || cnn.State != System.Data.ConnectionState.Open)
            {                
                try
                {
                    cnn.Open();
                    Console.WriteLine("connected.");                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error connect mysql");
                    Console.WriteLine(ex);
                }
            }
            return false;
        }
    }
}

Oh I get this error when I run the programm which uses my dll. If I use invalid props for the mysql connector string it will give me an error for that. But as soon as I call cnn.Open(); it crashes.

I have tried using newer and older versions of the mysql connector - without any success. The Mysql.Data packet did also not work. I also tried to use postgres and mongodb instead. But I always get the same kind of errors (just with different dll names). Any ideas?

(I also tried switching between 4.6, 4.7 and 4.8 without any success)

I would appreciate any help.. cause I really don't want to use MS SQL Server 2019..

1 Answers

This often means that the version of System.Diagnostics.DiagnosticSource in your project's bin folder is not the same as the one being referenced by MySqlConnector.

Find out what version is being copied into your bin folder, then add a binding redirect (similar to the following) to your project's Web.config:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-5.0.0.1" newVersion="5.0.0.1" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>
Related