Could not load file or assembly 'System.Data e

2020-07-03 07:45发布

问题:

The problem: When deploying my applications to the production database i get the following error:

Could not load file or assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

   at ConsoleApplication2.Database.DBConnect.Initialize(String username, String password, String IP)
   at ConsoleApplication2.Database.DBConnect..ctor(String username, String password, String IP) in C:\Users\Vincent\Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\DBConnect.cs:line 26
   at ConsoleApplication2.Database..ctor() in C:\Users\Vincent\Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\Database.cs:line 20
   at ConsoleApplication2.Main..ctor() in C:\Users\Vincent\Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\Main.cs:line 16

What i know: it isn't actually the System.Data.dll that has a problem.

Since I've started making a clean console application. First just using Console.write(). when that worked i made a new class and did some general computations (also without problems) than I decided to create a MySQL connection using the MySQL.data.MySqlClient; from the Mysql.data.dll this is when the error occured again. I got the dll from the mysql site.

the strange part: this dll has always worked on my computer (on which I created the applications) and on 2 different servers (1 of which I set up just for testing these applications to see if i would get the same problem)

some specs: my computer runs windows 7 32 bit. the production database runs windows 2003 service pack 2 as do the test server and the other server I've tried it on.

Some other stuff I've done: I have created an installer (.msi and .exe) both install .net 3.5 service pack 1 and .net 2.0 service pack 2.0 and .net 3.0 service pack 2. i found it strange that it would install all those .net frameworks but to install 3.5 you need 2.0 so that made sense (not sure about why it installed 3.0 though)

I've also tried running the application without the MySQL connector installed (so just place the mysql.data.dll in the application folder) and I've tried with the MySQL connector installed (so no dll's in the application folder)

I've tried copying my .net framework to the production database (this was before i knew it was about the mysql.data.dll and not the system.dll or system.data.dll)

I've tried everything i could think of yet nothing works, it works fine on all other computers/databases just not on the 1 we want to deploy the application...

The code where the error occurs:

        public DBConnect(string username, string password, string IP)
        {
            Console.WriteLine("dbconnect contsrutctor");
            Initialize(username, password, IP);
        }

        private void Initialize(string username, string password, string IP)
        {
            Console.WriteLine("initializing strings");
            string server = IP;
            string database = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + username + ";" + "PASSWORD=" + password + ";";
            Console.WriteLine("initializing mysqlconnection");
            //MySqlConnection connection = new MySqlConnection(connectionString);
        }

i have commented the line:

MySqlConnection connection = new MySqlConnection(connectionString);

and the applications runs without errors, when I un-comment it the error occurs again.

EDIT

I have noticed a strange thing: when ever it gets to the method (not the line) of where the MySqlConnection is created the error is thrown. If you would look at the above code sample the first Console.WriteLine("initializing strings"); does not even show in my console. I find this quite strange since you would expect the error to be thrown on the MySqlConnection connection = new MySqlConnection(connectionString); line and not at the start of the method.

EDIT 2 I thought i found an answer so i posted it:

"Though i have checked the system.data.dll and system.dll versions and file paths (which were exactly the same on all 3 machines I did find the problem to be with these files.

In my application I've set the "copy local" to true and after this it worked.

I find this extremely odd since I've tried replacing the dll's on the production server with my own and this did not work. also just placing the dll's in the application folder did not help either. I had to specifically specify the application to copy the dll's it self.

anyway it's working properly now although I'm not quite convinced i like this solution...

I definitely do not like this because i need to do this to half of the dll's (yes half not all just about half of them).

any better options are still welcome."

however I failed to mention it worked on my test application, the real application can't find system.transactions this time. so it's still not the right solution though it did clear the error about the system.data.dll i still can't run the program because of a similar error for a different file.

So since i'm still having the same error but on a different file (which i can't change the "copy local" setting to true since it's not referenced by me) i've deleted the answer and placed it here as an edit since the problem still isn't solved.

EDIT 3 I've run Fuslogvw.exe and this is the result:

* Assembly Binder Log Entry (12/5/2013 @ 8:43:13 AM) *

The operation failed. Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll Running under executable C:\Program Files\Custommate\email sorteer service\EmailSorteerService.exe --- A detailed error log follows.

=== Pre-bind state information === LOG: User = NAVMATE\Administrator LOG: DisplayName = System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 (Fully-specified) LOG: Appbase = file:///C:/Program Files/Custommate/email sorteer service/ LOG: Initial PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache Base = NULL LOG: AppName = NULL Calling assembly : EmailSorteerService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in default load context. LOG: No application configuration file found. LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Post-policy reference: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 LOG: GAC Lookup was unsuccessful. LOG: Attempting download of new URL file:///C:/Program Files/Custommate/email sorteer service/System.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Custommate/email sorteer service/System/System.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Custommate/email sorteer service/System.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Custommate/email sorteer service/System/System.EXE. LOG: All probing URLs attempted and failed.

Meaning it's trying to load the dll's from the folder i'm running the application from, i find this strange since i don't know what is causing this. (I have made sure copy local is set to false so that couldn't be the problem)

so the new question: how do i make sure it loads it from the right place?

回答1:

The reason why the exception is thrown as soon as you enter the method is simple - the JIT compiler only needs to resolve the references then. MySqlConnection requires System.Data, and it's the first method to do so, so that's when the exception is thrown.

As for debugging the issue, it does seem like improperly installed .NET framework on the target machine. You should try How to enable assembly bind failure logging (Fusion) in .NET to have a look at where .NET is actually trying to find the library, and why it discards any that it finds at all.

If it doesn't lead you to solve your problem, I'd try uninstalling and reinstalling .NET framework 2.0, and then 3.5.

The reason Copy Local sort of works is because then the application doesn't try to load the DLL from the Global Assembly Cache, but rather from the executable directory of your application. However, your real problem is that it isn't finding the right DLL in GAC.

If Fusion shows you that the MySQL library tries to load a wrong version of System.Data, you can use an application manifest to force it to load a different version (which should hopefully be compatible). You can read about application manifests here - http://msdn.microsoft.com/en-us/library/aa374191(VS.85).aspx

Good luck.



回答2:

Whenever I have seen issues like this, it is because I have not deployed the correct version of the library in question or because I am actually missing a library. I think others already referenced this but I wanted to repeat to emphasize that this is the main issue.

In order to solve these sorts of issues you can do two things. First, understand the order in which libraries are loaded:

  • Look in the Global Assembly Cache (GAC)
  • Search the folder your program is running from
  • Search the current directory
  • Search the system folder (System32 or SysWow64)
  • Search any other folders in the environment %PATH% variable

Once you understand that, you can look in each of these locations for wrong or conflicting versions of the library that you are referencing. Since System.Data is a library that comes with .NET I would guess that your build computer has a different version of .NET installed than you have on your computer you are trying to deploy to.

The other thing that I have found to be really helpful is a tool called fuslogvw.exe (http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx). If you follow the instructions on the FusionLog site you can trace .NET as it tries to load various assemblies. Simply enable the tool then check the logs file after you get the error. This should indicate where .NET was looking for the file and possibly why it failed.

As others said - best of luck!



回答3:

To find a which .dll it is ultimately failing to find ("or one of its dependencies" in the error message) and from where it is attempting to load it you can try Process Monitor from the SysInternals suite.

It's a simple download, unzip, and run in place. I suggest you then add a filter for your app's name (or whatever else you can use to uniquely identify it) and then just watch for it to try to load a .dll and fail. You can apply more filters to narrow down the list of events such as Operation is QueryNameInformationFile (first event it tries when locating a file). Note that for a lot of .dlls it will try a few places before ultimately finding a copy, so you're looking for a series of events referencing the same file name with no Success result at the end.

Best of luck.



回答4:

I had this error when Windows update was installing .Net 4.5.2 at the same time I was debugging my code in 4.5.1.



回答5:

I had the same problem,

First, you must copy your reference dll file into the bin folder of the main project that uses it and include the file at the references section of the main project inside VS.

Second, if you are using your main project as a library project and including it inside another project (a second project) then add the same reference dll file into the bin folder of this second project and also include this copied file at the references section of the second project at VS.

This workaround solved my problem, I hope it helps you too.



标签: c# mysql dll