mysql 在生产数据库上部署应用程序时无法加载文件或程序集“System.Data”错误

3hvapo4f  于 2023-03-22  发布在  Mysql
关注(0)|答案(5)|浏览(97)

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.

===预绑定状态信息===日志:用户= NAVMATE\管理员日志:DisplayName = System,Version=2.0.0.0,Culture=neutral,PublicKeyToken= b77 a5 c561934 e089(完全指定)日志:Appbase = file:///C:/Program Files/Custommate/email sorteer service/ LOG:初始私有路径= NULL日志:动态基=空日志:缓存库=空日志:AppName = NULL调用程序集:EmailSorteerService,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null. === LOG:此绑定在默认加载上下文中启动。日志:未找到应用程序配置文件。日志:使用C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.LOG中的计算机配置文件:政策后参考:系统,版本=2.0.0.0,文化=中性,PublicKeyToken= b77 a5 c561934 e089日志:GAC查找失败。日志:正在尝试下载新的URL file:///C:/Program Files/Custommate/email sorteer service/System.DLL。日志:正在尝试下载新的URL file:///C:/Program Files/Custommate/email sorteer service/System/System.DLL。日志:正在尝试下载新的URL file:///C:/Program Files/Custommate/email sorteer service/System.EXE。日志:正在尝试下载新的URL文件:///C:/Program Files/Custommate/email sorteer service/System/System.EXE。日志:尝试了所有探测URL,但均失败。
这意味着它试图从我运行应用程序的文件夹中加载dll,我觉得这很奇怪,因为我不知道是什么原因造成的。(我已经确保复制本地设置为false,所以这不可能是问题)
所以新的问题是我如何确保它加载它从正确的地方?

nbysray5

nbysray51#

当你输入方法时抛出异常的原因很简单--JIT编译器只需要解析引用。MySqlConnection需要System.Data,它是第一个这样做的方法,所以这就是抛出异常的时候。
至于调试问题,它看起来像是在目标机器上安装了不正确的.NET框架。你应该尝试How to enable assembly bind failure logging (Fusion) in .NET来看看.NET实际上在哪里试图找到库,以及为什么它会丢弃它找到的任何库。
如果它不能引导您解决问题,我会尝试卸载并重新安装.NET Framework 2.0,然后是3.5。
复制本地排序起作用的原因是因为应用程序不会尝试从全局程序集缓存加载DLL,而是从应用程序的可执行目录加载DLL。然而,真实的的问题是它没有在GAC中找到正确的DLL。
如果Fusion显示MySQL库试图加载错误版本的System.Data,您可以使用应用程序清单强制它加载不同版本(希望兼容)。您可以在此处阅读有关应用程序清单的信息-http://msdn.microsoft.com/en-us/library/aa374191(VS.85).aspx
祝你好运。

r8xiu3jd

r8xiu3jd2#

每当我看到这样的问题,这是因为我没有部署正确的 * 版本 * 的库的问题,或者因为我实际上错过了一个库。我想其他人已经提到了这一点,但我想重复强调,这是主要问题。
为了解决这类问题,你可以做两件事。首先,了解库的加载顺序:

  • 查看全局程序集缓存(GAC)
  • 搜索运行程序的文件夹
  • 搜索当前目录
  • 搜索系统文件夹(System32或SysWow 64)
  • 在环境%PATH%变量中搜索任何其他文件夹

一旦你理解了这一点,你就可以在这些位置中的每一个位置查找你引用的库的错误或冲突版本。由于System.Data是.NET附带的库,我猜你的生成计算机安装的.NET版本与你试图部署到的计算机上安装的版本不同。
我发现的另一件非常有用的事情是一个名为fuslogvw.exe(http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx)的工具。如果您按照FusionLog站点上的说明操作,您可以在.NET尝试加载各种程序集时跟踪它。只需启用该工具,然后在收到错误后检查日志文件。这应该表明.NET在哪里查找文件以及可能失败的原因。
就像其他人说的-祝你好运!

vptzau2j

vptzau2j3#

要查找最终未能找到的.dll(错误消息中的“或其依赖项之一”)以及它试图加载它的位置,您可以从SysInternals套件中尝试Process Monitor
这是一个简单的下载,解压缩,然后在适当的位置运行。我建议你为你的应用程序的名称添加一个过滤器(或任何其他您可以用来唯一标识它的东西),然后只要观察它是否尝试加载.dll并失败即可。(查找文件时尝试的第一个事件)。请注意,对于许多. dll文件,在最终找到副本之前,它会尝试几个位置,因此您正在寻找引用相同文件名的一系列事件,最后没有成功结果。
祝你好运。

fzwojiic

fzwojiic4#

当Windows更新安装.Net 4.5.2时,我在4.5.1中调试代码时遇到了这个错误。

gudnpqoy

gudnpqoy5#

我也有同样的问题,
首先,必须将引用dll文件复制到使用它的主项目的bin文件夹中,并将该文件包含在VS中主项目的references部分。
第二,如果您使用主项目作为库项目,并将其包含在另一个项目(第二个项目)中,则将相同的引用dll文件添加到第二个项目的bin文件夹中,并将此复制的文件包含在VS的第二个项目的引用部分。
这个解决方案解决了我的问题,我希望它也能帮助你。

相关问题